I am learning pointers, I learnt that an integer occupies 4 bytes so shouldn't the pointer pointing to an integer type data type mention all 4 addresses, instead of mentioning a single address?
The integer occupies four bytes, but has only one address.
I assume we're talking about C?
c
????
C is perhaps the most prolific programming language.
A pointer only contains the address of the first byte. Your program is smart enough to know that if you want to read an integer from that address, it should read four bytes, starting at that address. The effect of this is that you can also read each bytes of an integer (see the attached C file and remember that array accesses are basically pointer accesses). If you want to read a char from an address where you originally stored an int, that's perfectly possible. Your program will read one byte, starting from the address you supplied. This also makes it possible to change only the second byte of a four-byte integer. You can try to write a program that does that.
I am learning C/C++,but this was regarding C. Thanks @Slotema &@Ihm . Very helpful replies
Join our real-time social learning platform and learn together with your friends!