multidimention

This is a discussion on multidimention within the C Programming forums, part of the General Programming Boards category; hi all... 1. why this works: Code: int moo[2][MAX] = { {2,4,34,43,23,0}, {2,4,34,43,23,0} }; 2. but this doesn't: Code: int ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    18

    multidimention

    hi all...

    1. why this works:

    Code:
         int moo[2][MAX] = {
                    {2,4,34,43,23,0},
                    {2,4,34,43,23,0}
                    };

    2. but this doesn't:

    Code:
     int moo[2][MAX];
            moo = {
                    {2,4,34,43,23,0},
                    {2,4,34,43,23,0}
                    };

    when compiling 2 i get:
    error: syntax error before '{' token
    which points the line of: "moo = {" from 2.

    thanks...

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    { syntax is only valid when initializing an array at the same time you are declaring it.
    Once it is declared , you have to use the complete syntax -

    Code:
    moo[0][0] = 2;

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by toshog View Post
    hi all...

    1. why this works:

    Code:
         int moo[2][MAX] = {
                    {2,4,34,43,23,0},
                    {2,4,34,43,23,0}
                    };

    2. but this doesn't:

    Code:
     int moo[2][MAX];
            moo = {
                    {2,4,34,43,23,0},
                    {2,4,34,43,23,0}
                    };

    when compiling 2 i get:
    error: syntax error before '{' token
    which points the line of: "moo = {" from 2.

    thanks...
    The reason for the second one being incorrect is because "moo" is the equivalent to
    &moo[0][0] which is an adress not an element of the array, which is moo[row][column].
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    moo is equivalent to &moo[0], not &moo[0][0].

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    moo is the base address of the 2D array, which is address of moo[0][0], isn't it?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    C only has one-dimensional arrays, technically speaking. multidimensional arrays are made by making arrays of arrays. since the rule is "the name of an array is converted to a pointer to its first element" or somesuch, only the outer array is converted to a pointer. so it's a pointer to an array of MAX ints, or int (*)[MAX].

  7. #7
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    So, what will be moo in case it would've been a 3D array like moo[3][3][3]? Would it be still &moo[0]?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    yes, of type int (*)[3][3]. pointer to an array of three elements of an array of three elements of int.
    Last edited by robwhit; 07-26-2009 at 06:54 AM.

  9. #9
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Thanks. But I'm still a bit confused coz of this.
    Code:
    int a[3][3]={1,2,3,4,5,6,7,8,9};
    	printf("%p	%p	%p",a[1],&a[1],*(&a[1]));
    How the hell are all of the three values coming out to be equal? I know that a[1] is the address of 1st 1D array, but what about the rest of them?
    Last edited by BEN10; 07-26-2009 at 07:03 AM.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    the compiler probably uses a representation for "pointer to array" types that points to the first element of the array. so the only difference would be types.

    a[1] would be an int[3], which gets converted to an int *.
    &a[1] would be an int(*)[3], because the array-to-pointer conversion isn't applied when an array type is the operand of the & or sizeof operators. &a[1] and a[1] begin at the same place then, and if the representation of a pointer to an array is the starting place of the array, then they would both have the same "value".
    *(&a[1]) is the same as the first one. a[1] is an int[3], & gives an int(*)[3], * gives an int[3], and that gets converted to an int *.

    actually, that program has undefined behavior because %p is for void*, not any pointer type, although most compilers use the same representation for their pointer types, so it usually works.

    use:

    int a[3][3]={1,2,3,4,5,6,7,8,9};
    printf("%p %p %p", (void*)a[1], (void*)&a[1], (void*)*(&a[1]));
    Last edited by robwhit; 07-26-2009 at 07:28 AM. Reason: corrections x2

  11. #11
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by robwhit View Post
    *(&a[1]) is the same as the first one.
    Stupid me.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    fixed the explanation again.

  13. #13
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    This one makes it more clear. I also tried a[1]+1 and &a[1]+1 and understood the difference. Thanks.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21