Thread: Pointer Arrays

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    8

    Pointer Arrays

    When I run this code:
    Code:
    #include <stdio.h>
    
    int main() {
    	int x[] = {1,2,3,4,5,6,7,8,9,0};
    	printf("Address of x : %p\n", &x);
    	printf("Contents of x: %p\n", x);
    	return 0;
    }
    It outputs the same address twice. Why?
    It looks like x is pointing to itself.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The name of an array, used by itself, "decays" to a pointer to its first element. So &x and x have the same value, but different types (the first is the address of an array, the second is the address of an int).

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    8
    How is this held in memory?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    How is what held in memory? The array is just enough contiguous space for 10 ints.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    8
    x is a pointer to x[0], am I right?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by joezamboni View Post
    x is a pointer to x[0], am I right?
    Sure. (If you mean is there extra space set aside for that pointer, then no, there's no extra space there.)

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    8
    I dont understand how x can have a different value than x[0]. Where is x in relation to the data in the array?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by joezamboni
    I dont understand how x can have a different value than x[0]. Where is x in relation to the data in the array?
    Since x is an array, its value (or should I say values?) is all its elements.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    8
    I think I'm starting to understand. But, why does printf("Contents of x: %p\n", x); give me an address.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by joezamboni
    But, why does printf("Contents of x: %p\n", x); give me an address.
    Because in this context (and many others), an array is converted to a pointer to its first element.

    By the way, to be pedantically correct, you should write:
    Code:
    printf("Address of x : %p\n", (void*)&x);
    printf("Contents of x: %p\n", (void*)x);
    Since %p is supposed to correspond to a void pointer. However, since pointers to objects are convertible to pointers to void, this probably does not matter in practice.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Apr 2010
    Posts
    8
    But my question is, why does x contain it's own address?

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    x doesn't contain it's own address. None of x[0] through x[9] contain the address of x. The system has to store pointers to all the variables for its own use, and since you are not asking for the contents of x, but its location, the system is happy to tell you where that location is.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by joezamboni
    But my question is, why does x contain it's own address?
    It does not. The conversion to a pointer is just the way it works.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Apr 2010
    Posts
    8
    OK, thanks.

    One more thing: How would I print the value at x?

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by joezamboni
    How would I print the value at x?
    For example:
    Code:
    printf("%d\n", x[0]);
    EDIT:
    Actually, this is not quite correct. Put it in another way: suppose I told you: show me your hand! Which of your hands am I referring to (assuming that you have both of them)? Likewise, printing the value of x... which of the values of x?
    Last edited by laserlight; 04-02-2010 at 01:22 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. pointer arrays!!!
    By condorx in forum C Programming
    Replies: 1
    Last Post: 05-14-2002, 08:55 AM
  4. pointer arrays
    By condorx in forum C Programming
    Replies: 3
    Last Post: 05-03-2002, 09:04 PM
  5. Replies: 4
    Last Post: 11-05-2001, 02:35 PM

Tags for this Thread