pointer incremention | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

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)); }

11th Nov 2019, 5:41 PM
Far Had
Far Had - avatar
2 Answers
+ 5
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.
11th Nov 2019, 6:02 PM
Théophile
Théophile - avatar
+ 2
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]
11th Nov 2019, 6:57 PM
✳AsterisK✳
✳AsterisK✳ - avatar