Thread: pointers & arrays

  1. #1
    Registered User falconetti's Avatar
    Join Date
    Nov 2001
    Posts
    15

    Unhappy pointers & arrays

    Hello all !

    I am reading my first c book, and I could almost say "so far so good". Some serious doubts are starting to arise though. Most of them regarding pointers (I know you expected this ) and arrays.

    I know a pointer is a variable that contains a memory address as its value. I also know (tell me if I am wrong) that the name of an array is a pointer variable, pointing to the first element of the array. Well, I think I got that.

    But... letīs consider the following code:

    #include <stdio.h>

    int main (void)
    {
    int a [5] = {0,1,2,3,4};

    printf ("%p\n", &a [0]);
    printf ("%p\n", &a);

    return 0;
    } /* main */

    If 'a' is the name of the array, that is it is a pointer pointing to a[0], how can it be that it is located at same memory address as the first element in the array? I mean both a and a[0] are located at same memory address !!

    Any explanation will be appreciated.

    Thanks in advance !!

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    To tell you the truth, I'm a bit surprised that you are even allowed to use the & operator on an array.

    Basically, an array pointer is special because it is not a variable. When you use the & operator, you use it to reach the memory address of a variable. At compile-time however, the program can pretty much treat the array pointer as a constant, the array address isn't actually stored in memory, it's hard-coded into the executable.

    So, saying something like...
    &a
    is the same as saying...
    &0x8f23

    So I am no sure why we are allowed to use the & operator on array pointers, but apparantly we are, but since &a doesn't actually have any meaning, it gives a.

    Fortunately, the fact that it doesn't have any real meaning is coupled with the fact that it doesn't have much any use, so this isn't going to be a problem. Consider the two reasons to use the & operator...

    1) Because you want to call a function which modifies that variable's value (But you can't modify the value of an array pointer anyhow)
    2) Because a pointer to the value is smaller, memory-wise, than the value itself (But since an array aleady is a pointer value, a pointer to it is clearly not gonna be any smaller)

    A similar thing happens with 2-dimensional arrays...
    a[2][2]
    &a == &a[0] == &a[0][0]
    Because the two-dimensional-ness of the array is really just in our heads.. it's treated in memory the same as a one-dimensional array, the precompiler just runs through and changes it into a one dimensional array for us.
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Registered User falconetti's Avatar
    Join Date
    Nov 2001
    Posts
    15

    Thanks

    Thank you, QuestionC.

    I should have remembered that an array name actually is a pointer constant and not a pointer variable.

    Things seem much more clear now

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So I am no sure why we are allowed to use the & operator on array pointers, but apparantly we are, but since &a doesn't actually have any meaning, it gives a.
    Every variable has an address. In essence, all a pointer is, is an integer. Thus, it doens't actually care _where_ it points, so long as it's in the expected form: an address.

    And so:

    char *myptr, *pointer[], **ptr[], ***p[];

    You can get the address of all of these variables. Since you can, the program just sees: "ah, pointer, this is what I expect". I think.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Sayeh
    Guest
    To extend Quzah's explanation a little--


    Every location in RAM is located at a specific location. I know that's being redundant, but that helps make it more obvious.

    Therefore, since 'a' is in RAM, it therefore _must_ be located somewhere. So '&a' tells us where 'a' is located.

    Furthermore, 'a' and '&a' are NOT the same thing. 'a' is a pointer at location '&a'. In fact, for simplicity, let's say that 'a' is located at address 0x00500000 in ram. And let's further assume that 'a' is pointing to address 0x001A351C.

    Therefore:

    &a = 0x00500000
    a = 0x001A351C


    So if we look at the memory located at '&a', it looks like this:

    00500000 00 1A 35 1C
    00500004 ..

    --

    Okay. Let's say that 'a' is an integer array, single dimension. the members in 'a' are: {2537, 16, 34760, 1967}.

    If we look at the RAM at location 0x001A351C, we see the following values:

    001A351C 09 E9 00 10 87 C8 07 AF
    001A3524 ..

    or in other words:

    a[0] = 0x09E9 = 2537
    a[1] = 0x0010 = 16
    a[2] = 0x87C8 = 34760
    a[3] = 0x07AF = 1967

    So, to answer QuestionC's specific answer-- the reason you use '&a' from time to time, might be to compare the address of the array(s) in question. to see if they are the same, or different...

    enjoy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with returning arrays using pointers
    By cuba06 in forum C Programming
    Replies: 9
    Last Post: 11-23-2007, 10:40 AM
  2. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  3. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  4. pointers and arrays..
    By ahming in forum C Programming
    Replies: 1
    Last Post: 04-24-2004, 03:12 AM
  5. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 11:13 AM