Hi,
I am a beginner using C to program microcontrollers.
I have this problem: I have a variable (int, not a pointer ) representing an address and I have a data structure (array of int) .
How can I do the following operation:
x = (int)y;
printf("\nx: %x y: %p", x, y);
getch();
return 0;
}
I initially wrote code to make sure that sizeof(x) = sizeof so that x would be large enough to contain the value of y which is a pointer.
In this case, a DevC++ int is 4 bytes in size and the pointer is 4 bytes in size. always be careful, because on other systems and compilers these can vary. For example, in TurboC++ int's were 16 bits in size and you had far and near pointers which were different.
Anyway, the only real trick is to use a typecast, in this case typecasting the pointer into an integer with the x = (int)y; statement. Conversions like this are done all the time in c programming.
depends on the microcontroller .... if an address variable ie a pointer in your case has the same size as a long int or default int, you can do a typecast as shown in the previous post