+ 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)); }
3 ответов
+ 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.
+ 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]