简体   繁体   中英

Clarification on C pointers and memory addresses

I am interested in line 6, 7 and 8 in the code below.

#include <stdio.h>
#include <stdlib.h>

void go_south_east(int *lat, int *lon) {
  printf("Lat: %p, Long: %p\n", lat, lon);
  printf("Address of Lat: %p, Address of Long: %p\n", &lat, &lon);
  printf("Address of Lat: %p, Address of Long + 8 bytes?: %p\n", &lat, &lon+8);
  printf("Size of Lat: %lu, Size of Long: %lu\n", sizeof(lat), sizeof(lon));
  *lat -= 1;
  *lon += 1;
}

int main() {

  int latitude = 32;
  int longtitude = -64;
  go_south_east(&latitude, &longtitude);
  printf("Avast! Now at: [%i, %i]\n", latitude, longtitude);

  return 0;
}

The output I got was:

Address of Lat: 0x7fff5fbfe9e8, Address of Long: 0x7fff5fbfe9e0
Address of Lat: 0x7fff5fbfe9e8, Address of Long + 8 bytes?: 0x7fff5fbfea20 
Size of Lat: 8, Size of Long: 8

I understand that the size of the lat and long pointers are 8 bytes because they are long unsigned int. But why are they only 1 byte away from each other in the memory? Shouldn't they be 8 bytes away from each other since their size is 8 bytes? Please advice.


Thanks for all the helpful advice. I wished I could mark everyone's as answer but I can't. Really appreciate it.

There's a wild mix up of completely different things and unexplainable assertions in your question.

Firstly, I don't see an long unsigned int pointers in your code. All pointers in your code have int * type. Where did long unsigned int come from?

Secondly, lat and lon are data pointers. Typically, in a non-exotic C implementation all data pointers have the same size. It doesn't matter what they point to. On your platform data pointers have 8-byte size. That means that pointers to char and well as pointers to double as well as pointers to unsigned long int will have the same 8-byte size.

Thirdly, where did you get the idea that they are 1 byte away in memory? The very first line in your output clearly shows that they are located at 0x7fff5fbfe9e8 and 0x7fff5fbfe9e0 addresses. These addresses are exactly 8 bytes away: 0x7fff5fbfe9e8 minus 0x7fff5fbfe9e0 equals 8. So, where did your "1 byte away" come from?

Fourthly, your code seems to suggest that &lon+8 changes the address by "8 bytes". This is incorrect. Adding 1 to a data pointer T* shifts it by sizeof(T) bytes, which means that &lon+8 actually changes the address by 8*8=64 bytes, which is exactly what you observe in your output: 0x7fff5fbfea20 minus 0x7fff5fbfe9e0 equals 64.

Basically, the questions you ask are directly contradicting what you observe in your output. That's kinda makes it virtually impossible to answer. It is like showing people a red handkerchief and asking why it is green.

这两个地址之间的区别恰好是8,再看看。

They are eight bytes away from one another. PCs are byte addressable, not bit addressable.

That is to say that the numbered memory units are bytes .

Pointer arithmetic actually adds the size of the data type to the address. To clarify:

int* i = 0;
i++;
printf("%p\n", i); // Prints 0x4, not 0x1

So

printf("Address of Lat: %p, Address of Long + 8 bytes?: %p\n", &lat, &lon+8);

Actually prints the address of lon + 8 * sizeof(&lon) bytes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM