Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the difference in memory address' of two elements of a char array 4?

Tags:

arrays

c

memory

I was messing around in C trying to find a way to interpret the difference in memory address' when finding two array elements next to each other and finding the difference. Here's my code:

#include <stdio.h>

int main(){
  char *a[5];
  printf("%p\n", (&a[0]));
  printf("%p\n", (&a[1]));
  return 0;
}

And example output:

0xbf9343dc
0xbf9343e0

When doing this, shouldn't the output be something to the form:

0x0....0
0x0....1

Because the size of a char pointer should be 1 (on a 32-bit system which I am using).

If anyone knows why this is the case and can offer an explanation it would be very helpful

like image 322
Sythe Avatar asked Dec 29 '25 13:12

Sythe


1 Answers

The size of a char is 1, the size of a char * is implementation dependent, usually 4 in 32-bit system.

like image 119
ouah Avatar answered Jan 01 '26 04:01

ouah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!