-4

why value a and x is different? I think it should be the same

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

int main(int argc, char *argv[]) {
   
  int a =5;
  int *x=&a;

  printf("%ld\n", sizeof(a)); // print 4
  
  printf("%ld\n", sizeof(x)); // print 8
   
   return(0);
}
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • 10
    Why do you think they *would* be the same? – dbush Apr 16 '22 at 02:46
  • 1
    A pointer must be large enough to be able to point to all possible memory addresses. On 64-bit systems, this will tend to be 8 bytes. – JohnFilleau Apr 16 '22 at 02:48
  • @dbush: Surely you'd never need more than 256 different `char*`s in your entire program! ;-) – ShadowRanger Apr 16 '22 at 02:51
  • 2
    Sidebar: the correct `printf` field descriptor for a `size_t` (the type of the result of `sizeof`) is `%zu`. – John Bollinger Apr 16 '22 at 03:05
  • On any given system the size of a pointer is the same no matter what type it points to. Variables of various types---`char`, `int`, `long`, etc.--- may be different. – jkb Apr 16 '22 at 04:22
  • 1
    @jkb: That is actually not quite correct. The ISO C and ISO C++ standard [allows pointers to different types to be of different sizes](https://stackoverflow.com/q/71870205/12149471). Historically, there have actually been cases where pointers to different types were actually handled differently and had different sizes. However, I have never encountered such a system myself, I have only read about them. – Andreas Wenzel Apr 16 '22 at 06:47
  • @AndreasWenzel Systems with different sizes for objects pointers vs. function pointers are not unusual these days. Perhaps you have come across those? – chux - Reinstate Monica Apr 16 '22 at 08:49
  • @JohnFilleau "A pointer must be large enough to be able to point to all possible memory addresses.' --> C does not require that - counter and historic examples exist. – chux - Reinstate Monica Apr 16 '22 at 08:52
  • @chux-ReinstateMonica thanks -- I'm going to leave my comment up so your response makes sense in context. – JohnFilleau Apr 16 '22 at 13:18

2 Answers2

1

I am not sure whether you are aware of what you are actually measuring with the sizeof operator.

First you measure integer a, which is the size of an int (4 bytes). And second you measure not an integer but a pointer which stores the address of integer a, so the value is different and I think the value of x is 8 bytes.

  • int a (4 bytes INTEGER)
  • int *x = &a (is address of a)

But I can be wrong.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
kubatron
  • 11
  • 3
  • On a 64-bit system, the value of a pointer is normally 8 bytes, but on a 32-bit system (or in a 32-bit executable running on a 64-bit system), the size of a pointer is normally 4 bytes. – Andreas Wenzel Apr 16 '22 at 06:57
0

why value a and x is different? I think it should be the same

a is an int and x is a pointer.

int a = 5;
int *x = &a;

C does not specifier an int and a pointer need to be the same size. They may have the same size. Commonly a pointer is as wide as an int or wider, yet either one may be wider than the other.

It is implementation defined.


Since C99, use "%zu" to print a size_t, which is the resultant type from sizeof.

// printf("%ld\n", sizeof(a));
printf("%zu\n", sizeof(a));
// or 
printf("%zu\n", sizeof a); // () not needed about objects.
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256