Character pointers and integer pointers (++)

前端 未结 10 1346
终归单人心
终归单人心 2020-11-29 11:50

I have two pointers,

char *str1;
int *str2;

If I look at the size of both the pointers let’s assume

str1=4 bytes
str2=4 byt         


        
10条回答
  •  遥遥无期
    2020-11-29 12:23

    Pointer increment always increases the address it points to by the size of the type represented by it. So, for char pointer it increments by 1 and for integer by 4. But, pointer variable itself will require 4 bytes to hold the address.

    You can inturn think of how array indexing works. Incase of an integer array a[0] will point to first element and a[1] will point to second. In this case for increment of 1 it should increment by 4 bytes to access the next integer. In case of characters it has to be 1. The same concept works for all pointer arithemtic.

提交回复
热议问题