#include <stdio.h>
const char *c = "hello";
const char *cp = (unsigned char*)&c;
const char *cpp = (unsigned char*)&cp;
int main (){
printf("PTR c %p \n",c);
printf("PTR cp %p \n",cp);
printf("PTR cpp %p \n",cpp);
printf("\n\n");
printf("CONTENTS cp 0x%x \n",*(unsigned int*)cp);
printf("CONTENTS cpp 0x%x \n",*(unsigned int*)cpp);
printf(" \n\n Demonstrating pointer arithmetic. \n\n");
printf("PTR c %p \n ",c);
printf("PTR (c+1) %p \n ",(c+1));
printf("PTR c %p \n ",(unsigned int*)c);
printf("PTR (c+1) %p \n ",(unsigned int*)(c+1));
printf("PTR c %p \n ",(unsigned long*)c);
printf("PTR (c+1) %p \n ",(unsigned long*)(c+1));
return 0;
}
The output of the program is given below
PTR c 0x4007a0
PTR cp 0x601028
PTR cpp 0x601030
CONTENTS cp 0x4007a0
CONTENTS cpp 0x601028
Demonstrating pointer arithmetic.
PTR c 0x4007a0
PTR (c+1) 0x4007a1
PTR c 0x4007a0
PTR (c+1) 0x4007a1
PTR c 0x4007a0
PTR (c+1) 0x4007a1
If you look at the portion Demonstrating pointer arithmetic,I would expect the following results
1) The first two lines print 'char pointers' one address apart,hence the difference should be '1' - which is what we are getting
2) The next two lines print 'int pointers' one address apart,hence the difference should be '4' - WHAT WENT WRONG??
3) The next two lines print 'long pointers' one address apart,hence the difference should be '4/8' - WHAT WENT WRONG??