Pointer arthematic . Its simple but i am confusing me. wt is diff b/w p+1 and &p+1

This is a discussion on Pointer arthematic . Its simple but i am confusing me. wt is diff b/w p+1 and &p+1 within the C Programming forums, part of the General Programming Boards category; Hi , what is the difference between p+1 and &p+1 from below program. I understand that p+1 increase to next ...

  1. #1
    kotin
    Join Date
    Oct 2009
    Posts
    132

    Pointer arthematic . Its simple but i am confusing me. wt is diff b/w p+1 and &p+1

    Hi ,

    what is the difference between p+1 and &p+1 from below program. I understand that p+1 increase to next value and &p+1 points to value after sizeof(p).

    my question the value of p and &p are same. But why final value comes to differ?

    Code:
    int main()
    {
    	char *p[]={"hai","howareyou"};
    	printf ("%d\n",p+1);
    	printf ("%d\n",&p+1);
    	return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    What is the type of p and what is the type of &p?
    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

  3. #3
    kotin
    Join Date
    Oct 2009
    Posts
    132
    p is character pointer to an array. &p is address of pointer to any array.

    and i getting the output as

    1075588900
    1075588896

    I understand why output is coming like that. But i did not get why it is coming like this. Because value of p and &p are same right?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    Quote Originally Posted by nkrao123@gmail.
    p is character pointer to an array.
    No, p is an array of 2 pointers to char. It is converted to a pointer to a pointer to char.

    Quote Originally Posted by nkrao123@gmail.
    &p is address of pointer to any array.
    Since p is an array of 2 pointers to char, the type of &p is a pointer to an array of 2 pointers to char.

    Quote Originally Posted by nkrao123@gmail.
    I understand why output is coming like that. But i did not get why it is coming like this.
    This sounds like a contradiction

    Quote Originally Posted by nkrao123@gmail.
    Because value of p and &p are same right?
    Yes, but I asked you about the types involved because that determines what the resulting addresses will be. Given an array, if a pointer x points to the first element of the array, (x + 1) points to the second element of the array, if it exists. The address of the first element of the array and the address of the second element of the array differ by the sizeof an element of the array.

    In your example, the reason for the difference is that sizeof of a pointer to char is not the same as sizeof an array of 2 pointers to char.
    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

  5. #5
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Really i am confusing.

    if i take the below example
    Code:
    int main()
    {
    	char a[]={1,2,3};
    	printf ("%d\n",a+1);
    	printf ("%d\n",&a+1);
    	return 0;
    }
    in above code also a and &a values are same but the valueof a+1 and &a+1 are different .

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,313
    Correct. Pointer arithmetic adds the size of the object pointed to.

    a + 1 is of type pointer to char. sizeof(char) is 1, by definition, so a + 1 is equal to &a[1].

    &a + 1 is of type pointer to array of three char. sizeof(array of three char) is equal to 3, so &a+1 has value equal to &a[3]. (and, yes, a[3] does not exist).

    The same style of logic applies to the example in your first post, except your array of of type array of pointer to char.

    Incidentally, use %p to print the value of a pointer, not %d. Converting the value of a pointer to int (which is what happens when you use %d) can change the value.
    Right 98% of the time, and don't care about the other 3%.

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Take a read through Prelude's tutorial on pointers. It is one of the best around and should answer most of your questions.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple maybe, but confusing at first.
    By Queatrix in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 09-17-2006, 11:54 AM
  2. Confusing pointer
    By vaibhav in forum C++ Programming
    Replies: 9
    Last Post: 11-13-2005, 04:52 PM
  3. Confusing Pointer
    By loko in forum C Programming
    Replies: 4
    Last Post: 08-29-2005, 08:52 PM
  4. confusing about pointer..
    By cBegginer in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 01:30 AM
  5. Simple but confusing.......
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 08-03-2002, 10:15 AM

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