-3

Please have a look at below code

#include<stdio.h>
int main(void){
  int *ptr,a,b;
  a = ptr;
  b = ptr + 1;
  printf("the value of a,b is %d and %d respectively\n",a,b);


  printf("the value of a is %d \n",(ptr));

  printf("the value of b is %d \n",(ptr+1));

    printf("the value  of (ptr+1)-ptr is %d \n",(ptr+1)-ptr);
  return 0;
}

Output:

the value of a,b is 0 and 4 respectively
the value of a is 0 
the value of b is 4 
the value  of (ptr+1)-ptr is 1 

I am not able to understand why the value of (ptr+1)-ptr is 1 not 4 as 4-0?Is it due to computation optimization?

Avaneesh Kumar
  • 143
  • 1
  • 7
  • 2
    You really need a good Book of C Language and you need to read a lot about pointers. By the way this: `int *ptr,a,b;` is the same as: `int *ptr; int a; int b;`. Now what do you think is the meaning of this: `a = ptr;` and this: `b = ptr + 1;` ? – Michi Mar 01 '18 at 15:26
  • `int *ptr,a; a = ptr;` is _undefined behavior_ (UB) – chux - Reinstate Monica Mar 01 '18 at 15:30
  • 3
    What you do not seem to understand is that you should _turn warnings on_ of your compiler and act on those warnings. Everything will become clear then. – Paul Ogilvie Mar 01 '18 at 15:30
  • You got ptr+1 = 4 , ie. 0+1 =4 and you didnt had problem. And now you get (ptr+1) - ptr = 1, and you have a problem. strange . – Shihab Pullissery Mar 01 '18 at 15:33
  • 1
    An 'optimization' should never change the results so that can't be it. – H H Mar 01 '18 at 15:50

2 Answers2

5

First of all, what you did is wrong. Pointer arithmetic is valid if both of them point to elements of the same array object or one past the last element of the array object. So it is undefined behavior.

In your case, the subtraction returns how far one is from the other - based on the type of the object pointed to which is int here, and sizeof int being 4 bytes, it returned 1 denoting the 1 int separation. (meaning the same as 4 byte - as can be inferred from the value of a and b).

From §6.5.6¶9 C11 standard (this points out both the points mentioned above)

When two pointers are subtracted, both shall point to elements of the same array object, or one past the last element of the array object; the result is the difference of the subscripts of the two array elements...

And you are printing addresses with %d format specifier. You should use %p format specifier with argument void* casted. And never think of dereferencing these pointers as it would be Undefined Behavior to do so.

user2736738
  • 30,591
  • 5
  • 42
  • 56
1

I am not able to understand why the value of (ptr+1)-ptr is 1 not 4

No, pointer arithmetic in C happens in units related to the pointed type (not in bytes). Read some C programming book.

So (ptr+1) - ptr is 1, but if you cast each pointer to a char*, e.g. by coding (char*)(ptr-1) - (char*)ptr, you'll get probably 4 (assuming sizeof(int) is 4).

Don't forget to enable all warnings and debug info when compiling: if using GCC, compile with gcc -Wall -Wextra -g, then improve your code to get no warnings at all. Read more about undefined behavior (your code have some) and be scared of it. Use the gdb debugger (or whatever other debugger you can use). Read documentation of every standard function that you are using (and of every external function from some other library), notably of printf.

Later, consider also reading pieces of the C11 standard, e.g. n1570 (a draft which is actually the standard text).

Care about portability (to different computers or compilers).

By definition, compiler optimization should not change the observed behavior and semantics of your program.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547