Thread: How Are C Arrays Represented In Memory?

  1. #16
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    For comparison, xptr and &xptr here are different:

    Code:
    #include <stdio.h>
    
    int main()
    {
        int x = 123;
        int *xptr = &x;
     
    	printf("x       %d\n",x);
    	printf("&x      %p\n",&x);
    	printf("xptr    %p\n",xptr);
    	printf("&xptr   %p\n",&xptr);
    
    	return 0;
    }
    
    output:
    
    x       123
    &x      0012ff84
    xptr    0012ff84
    &xptr   0012ff80
    I am wondering about the difference between the pointer xptr just above,
    and the conceptual pointer x from the array x[].

  2. #17
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    On line 10 you are printing the value of xptr
    on line 11 you are printing it's address.

    Hence ... different numbers.

  3. #18
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Yes I know, that's what I expect.

    With arrays, the array name yields the same address as the "address of" array name.

    The x in x[] has the same value as &x.

  4. #19
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    So what exactly is your question?

  5. #20
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    An array name, the x in x[] for example can be treated like a pointer.

    But the address of that "pointer" equals the value of that pointer.
    Why the difference between an array name when used as a pointer, and an actual pointer?

  6. #21
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Pointers are not arrays ... however, in some cases, arrays are treated as pointers.

    That is x[i] acts like adding an offset to the pointer and dereferencing it... functionally it equates to *(x + i);

  7. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I can't believe you've gotten this far without this: Arrays and Pointers


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

  8. #23
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Thanks, that explained it:

    Arrays and Pointers

    I've used array names as a pointers before, but never even considered the address
    of array name. Never needed it.

  9. #24
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You are needlessly confusing yourself about what &x means. In any code written by anyone with half a clue, you would never see such a construct. "Address of an array" is the same as "Address of the first element of the array" is the same as x.

    I wish they'd just have made it an error to even do that. You should never see it in real code and you should DEFINITELY never write it.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  2. How does 0xff get represented on a 16 bit machine?
    By Overworked_PhD in forum C Programming
    Replies: 2
    Last Post: 10-27-2007, 11:32 AM
  3. Array of Booleans Represented with 1 Bit
    By Brad0407 in forum C++ Programming
    Replies: 43
    Last Post: 07-08-2007, 01:23 PM
  4. memory and arrays
    By rwmarsh in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2006, 11:40 AM
  5. Permanent Memory Arrays
    By EricR in forum C++ Programming
    Replies: 6
    Last Post: 04-12-2005, 01:59 PM

Tags for this Thread