liv2hak
liv2hak

Reputation: 14970

Attempting to understand pointer arithmetic

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

Upvotes: 2

Views: 198

Answers (2)

ziollek
ziollek

Reputation: 1993

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

Upvotes: 0

Marco A.
Marco A.

Reputation: 43662

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

Upvotes: 4

Related Questions