pointer incremention | SoloLearn: Learn to code for FREE!


pointer incremention

In ghis code, ptr is of type "pointer" or "hex" and i is of type "int". How is this possible to add an integer to a hex number and get to the next element of the array? int a[5] = {22, 33, 44, 55, 66}; int *ptr = NULL; int i; ptr = a; for (i = 0; i < 5; i++) { printf("%d ", *(ptr + i)); }

11/11/2019 5:41:45 PM

Far Had

3 Answers

New Answer


Because the compiler translates all into binary. Thus, hex values and decimal values are converted in binary. So there is no problem. And "ptr" isn't of type "hex". An hexadecimal / decimal / binary value is only a representation of number, not a type.


Take particular note of the fact that although we are adding 1 to the pointer the physical address is incremented by the size of an integer. To see what I mean change the line... printf("%d ", *(ptr + i)); to... printf("%d\n", (ptr + i)); this will display the actual addresses.


ptr is a pointer of type int which hold the address of an int or an array of int, "a" is now assigned to ptr which means ptr is pointing to the address of the first value in the array, the array can then be use to access the array values just as you have seen, the pointer will be able to access this easily because the memory block of each values in the array are layed contigeously, so ptr+1 means the next value just as a[1]