basic of array and pointer

This is a discussion on basic of array and pointer within the C Programming forums, part of the General Programming Boards category; I have this below code Code: int main() { int x[10] = {0, 1, 2, 3, 4, 5, 6, 7, ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    1

    Question basic of array and pointer

    I have this below code

    Code:
    int main()
    {
    	int x[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    	int *pointer = NULL;
    
    	pointer = x[4];
    	printf("%d \n", pointer);
    
    	pointer = &x[4];
    	printf("%d \n", *pointer);
    
    	return 0;
    }
    So my question is what is the difference between, I kind of get confused.
    Code:
    pointer = x[4];
    	printf("%d \n", pointer);
    and
    Code:
    pointer = &x[4];
    	printf("%d \n", *pointer);

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Your example is kind of a bad/tricky one because this line:
    Code:
    pointer = x[4];
    is possible but it is bad syntax (will generate a gcc warning) and no one would really do this in real life. If you try to access the memory pointed to by this pointer now it will segfault. However, the next printf statement does not do that; it prints the "memory address" stored in the pointer itself. In this case, it is set to "4" which is not a valid memory address. So this is misuse of the type. If you check the address pointed to this way:
    Code:
    printf("%p\n",pointer);
    It will give you that (invalid) address in hex (0x4).

    The second example would be proper use of an int pointer:
    Code:
    pointer = &x[4];
    printf("%d \n", *pointer);
    The first line assigns the address of (&) x[4] to "pointer". So now if you check the address contained in pointer (with "%p" again), you will get a real address (something like "0x7fff959dccf0"); that is the address of x[4]. "pointer" points to x[4]. So when you ask for the value of the memory pointed to by dereferencing (*):
    Code:
    printf("%d \n", *pointer);
    You get 4.

    In short, the first example = WRONG.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Read The C Programming Language, that is, the book on the actual definition of the C language (you can probably get a PDF). Pointers didn't really make sense until I read that - it helps alot!

  4. #4
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    I think this one is also good for this specific topic: A Tutorial on Pointers and Arrays

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

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