Pointer assignment using array name

This is a discussion on Pointer assignment using array name within the C Programming forums, part of the General Programming Boards category; Could someone please explain why the pointer ptr assignment causes ptr to point at one memory location past the array ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    13

    Pointer assignment using array name

    Could someone please explain why the pointer ptr assignment causes ptr to point at one memory location past the array last element?

    I have output the memory locations just for verification. I thought that &a could point to the first element of the array.

    0022FF60 0022FF64 0022FF68 0022FF6C
    0022FF70
    4 3

    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int  a[4] = {1,2,3,4};
      int *ptr = (int*)(&a+1); // Why does this assignement put the ptr pointer
    						   // to point at one memory location after the array???
      int *p = a;
    
      printf("%p %p %p %p\n",p,(p+1),(p+2),(p+3));
      printf("%p \n",ptr);
      printf("%d %d", *(a+3), *(ptr-2) );
      
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    Actually,you're changing the value of the address of an array a with the following statement in your program.

    Code:
    int *ptr = (int*)(&a+1);
    The above statement makes the pointer of an array which address is 2000 will be changed to 2016 where no element points.The value is incremented by 16 because the array itself can store four elements which is 4*4=16.That's why it is giving garbage value.If you want the pointer to point to first element of the array.You use the following code.

    Code:
    int *ptr=(int*)(&a);
    If you want the pointer to point to second element of the array.You use the following code.

    Code:
    int *ptr=(int*)(&a)+1;
    Last edited by vivekraj; 03-12-2010 at 02:51 AM.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    13
    Thanks for your answer but I still do not understand.

    What is the difference of

    Code:
    int *ptr = (int*)(&a+1);
    and that of

    Code:
    int *ptr = (int*)(&a)+1;
    since the & operator has a higher priority of the + operator?

    If taking the &a, we take tha address of the array (actuallly the address of the first element), then this address plus one should point to the second element, right?

    Why does it point to the next memory location past the array?

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    Code:
    int *ptr = (int*)(&a+1);
    The above statement will take the size of an array which is 16 bytes and when it is incremented by 1,16 bytes will be added to it.

    In the case of second statement,

    Code:
    int *ptr = (int*)(&a)+1;
    The above statement will take the size of an integer pointer which is 4 bytes not the size of an array which is 16 bytes.When it is incremented by 1,4 bytes will be added to it.So,it will point to the second element of the array because expressions within () will be executed first.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    13
    Ok, I understand.

    I had not seen up to now such a definition where you can take the whole size of an array (besides "sizeof" operator) and add a multiple integral of such size to it.

    This is what confused me.

    This was an exercise I saw and tried to solve. I wonder if someone can actually take advantage of such assignments in a real program.

    Why should someone want to add multiple times the size of an array??

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,910
    Actually, if you want a pointer to point to the second element of the array a, you should write:
    Code:
    int *ptr = a + 1;
    This is because an array is converted to a pointer to its first element. Adding 1 to such an array results in a pointer to the second element.

    &a really does give you the address of the array a. It so happens that this address is equal in value to the address of the first element of a, but in terms of type the pointers are not the same, i.e., &a is an int(*)[4], whereas a when converted to a pointer to its first element is an int*.

    Quote Originally Posted by stavos77
    This was an exercise I saw and tried to solve. I wonder if someone can actually take advantage of such assignments in a real program.

    Why should someone want to add multiple times the size of an array??
    If you are dealing with a two dimensional array, then traversing the elements of such an array involves traversing over arrays.
    Last edited by laserlight; 03-12-2010 at 03:55 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    13
    Laserlight you are great.

    Now I really understand that. "&a" and "a" are pointing to the same element (first of array) but they are different types of pointers.

    Multidimensional arrays could certainly use this technique...you are right.

    Thanks

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,433
    Quote Originally Posted by stavos77 View Post
    Now I really understand that. "&a" and "a" are pointing to the same element (first of array) but they are different types of pointers.
    No.

    The element pointed to by &a is the array. So (&a + 1) points past the end of the array.

    The element pointed by by a is the first element of the array (i.e. the int with a value 1). So a + 1 points to the second element of the array (i.e. the int with a value 2).

    The fact the code won't compiler unless you use a conversion in this line
    Code:
       int *p = (int *) (&a + 1);
    tells you that &a + 1 is not a pointer to an int.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-15-2009, 08:38 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Pros pls help, Pointer to array
    By kokopo2 in forum C Programming
    Replies: 7
    Last Post: 08-17-2005, 11:07 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. sending or recieving a[][]to a function
    By arian in forum C++ Programming
    Replies: 12
    Last Post: 05-01-2004, 10:58 AM

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