Find the answer to your Linux question:
Results 1 to 2 of 2
Hi friends, I have a major doubt in array concept. For eg i declare int a[10]; int b[10][10]; I am confused how &a and a are equal (&a = a) ...
  1. #1
    Just Joined!
    Join Date
    Mar 2007
    Posts
    15

    Question Doubt in arrays

    Hi friends,

    I have a major doubt in array concept.
    For eg i declare
    int a[10];
    int b[10][10];

    I am confused how &a and a are equal (&a = a)
    and
    how &b=b=*b..................

    I would be thankful to any one who could explain me the logic behind this....

    Please help friends.......

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Pointers and arrays work similarly, but not quite identically.

    If you declare something as an array, then using "something" by itself in a C program acts as a pointer to the first element of that something.

    If you declare something as an array, then &something is the address of the first element of that something. So in this case, something and &something mean the same thing.

    That takes care of your "a" case.

    Your "b" case is similar. But b is an array of arrays, right? So b[0] is the first array within the array of arrays; b[1] is the second array; and so on.

    So *b is the first array within the array of arrays, just as *a is the first element of a.

    A simple reference to an array works like a pointer to the first element of that array. Since *b is an array, *b is equivalent to b[0], which is an array, so it works like a pointer to the first element of that array, which means a pointer to b[0][0].

    Try this simple program and watch the magic:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int a[10];
      int b[10][10];
    
      a[0]=9;
      b[0][0]=8;
    
      printf("0x%08X 0x%08X 0x%08X\n",
             &a,
             a,
             *a
            );
    
      printf("0x%08X 0x%08X 0x%08X 0x%08X\n",
             &b,
             b,
             *b,
             **b
            );
    
      return 0;
    
    } /* main() */
    If this explanation is insufficient, you may wish to become more grounded in how arrays and pointers work in C. To do so, google this:
    Code:
    C pointer array
    and you'll get a whole bouquet of tutorials on the subject (along with other stuff that doesn't apply).

    Hope this helps.

Posting Permissions

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