Results 1 to 7 of 7

Thread: How do I address C variables that use an typedef array in an array?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,127

    How do I address C variables that use an typedef array in an array?

    For example, if I have a type like this "typedef int TwoInts[2];" and then I have an array of this type like this "TwoInts MyVar[10]", how do I address the individual elements of that array?
    Do I do MyVar[y][x], where y is an entry in the MyVar array (the entry being of type TwoInts), and then x selects which one of the 2 int fields within this array entry? Or would y select the int field, while x would select the entry in the array?

  2. #2
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    801

    Re: How do I address C variables that use an typedef array in an array?

    Do you mean something like:

    Code:
    #include <stdio.h>
    
    typedef int TwoInts[2];
    
    int main() {
    	TwoInts MyVar[10] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 };
    
    	printf("%i %i\n", MyVar[1][0], MyVar[1][1]);
    	printf("%i %i\n", MyVar[3][0], MyVar[3][1]);
    }
    which displays:

    Code:
    3 4
    7 8
    [3] selects the element in MyVar and then [0] or [1] selects the element of that as per TwoInts.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.4.2)

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    3,508

    Re: How do I address C variables that use an typedef array in an array?

    Quote Originally Posted by Ben321 View Post
    For example, if I have a type like this "typedef int TwoInts[2];" and then I have an array of this type like this "TwoInts MyVar[10]", how do I address the individual elements of that array?
    Do I do MyVar[y][x], where y is an entry in the MyVar array (the entry being of type TwoInts), and then x selects which one of the 2 int fields within this array entry? Or would y select the int field, while x would select the entry in the array?
    Speaking in VB6 this would be:
    Dim Myvar(0 To 9, 0 To 1) As Long
    So y would be first dimension, x the second
    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  4. #4
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    4,321

    Re: How do I address C variables that use an typedef array in an array?

    In VB6 dimensions are reversed i.e. y have to be the second one in order to keep the memory layout like in C/C++

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    3,508

    Re: How do I address C variables that use an typedef array in an array?

    Quote Originally Posted by wqweto;[URL="tel:5586748"
    5586748[/URL]]In VB6 dimensions are reversed i.e. y have to be the second one in order to keep the memory layout like in C/C++
    Damn! That‘s one of those „50:50“ things i can never remember.
    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  6. #6
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    801

    Re: How do I address C variables that use an typedef array in an array?

    Given the C++ code above, then effectively you have:

    Code:
    TwoInts temp = MyVar[3];
    int myInt = temp[1];
    c/c++ has arrays of 1 dimension. Although you can have an array of type array of type array.... which allows multi-dimensions to be used. The types of each 'array' can be different.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.4.2)

  7. #7
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    4,321

    Re: How do I address C variables that use an typedef array in an array?

    Quote Originally Posted by Zvoni View Post
    Damn! That‘s one of those „50:50“ things i can never remember.
    In VB6 it's the natural x,y while in C/C++ you have to be able to get a pointer to y-th row with MyVar[y] so it's reversed to MyVar[y][x]

    You can check which dimension is which in VB's layout like this

    Code:
        Dim Myvar(0 To 9, 0 To 1) As Long
        Debug.Print VarPtr(Myvar(1, 0)) - VarPtr(Myvar(0, 0))
        '--> 4
        Debug.Print VarPtr(Myvar(0, 1)) - VarPtr(Myvar(0, 0))
        '--> 40
    Obviously the second dimension "offsets" by 40 bytes so it must be "by row"

    cheers,
    </wqw>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width