2
#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??

liv2hak
  • 14,472
  • 53
  • 157
  • 270
  • 1
    When you do `&c` you get a pointer to `c`, so `&c` is a pointer to a pointer to `const char`. – Some programmer dude Feb 26 '14 at 19:57
  • 2
    I think you're incrementing before casting – Marco A. Feb 26 '14 at 19:58
  • This code is absolutely insane. `const char *cpp = (unsigned char*)&cp;` makes _no sense at all_. – Mooing Duck Feb 26 '14 at 20:09
  • @Mooing Duck - why not? – liv2hak Feb 26 '14 at 20:46
  • 1
    Taking a `const char*`, taking the address of that (a `const char**`), explicitly casting that to a `unsigned char*` (which is dangerous and pointless), and then implicitly casting that to a `const char*` (equally dangerous and pointless). It looks like what you wanted was `const char** cpp = &cp;` Tip: If you have type casts in C++, your code is probably wrong. – Mooing Duck Feb 26 '14 at 20:55

2 Answers2

4

You are incrementing the values before casting them to the wanted pointer

To interpret 'c' as another pointer you should rather do

printf("PTR    (c+1)  %p \n ",(unsigned int*)c+1);

otherwise the parenthesis will give priority to the increment before the casting.

Without those parenthesis, casting has higher precedence than addition, like in the following

item = (char*)heap + offset;

that is equivalent to

item = ((char *)heap) + offset

Reference: precedence table C type casts and addition precedence

Community
  • 1
  • 1
Marco A.
  • 43,032
  • 26
  • 132
  • 246
0

You should cast var to correct pointer before add them const value. Also i suggest cast type to unsingned long long to determine 64bit width:

    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 long*)c);
    printf("PTR    (c+1)  %p \n ",((unsigned long long*)c+1));

and the result is:

PTR     c     0x80485f0
 PTR    (c+1)  0x80485f1
 PTR     c     0x80485f0
 PTR    (c+1)  0x80485f4
 PTR     c     0x80485f0
 PTR    (c+1)  0x80485f8
ziollek
  • 1,973
  • 1
  • 12
  • 10