1
$\begingroup$

Why sizeof(int) and sizeof(int *) takes different value in a system? I mean generally size of integer is 4 B, but size of integer pointer is 8 B. But why they cannot take same value, although their return type is same?

$\endgroup$
  • 1
    $\begingroup$ What do you mean by "their return type is the same"? $\endgroup$ – Yuval Filmus Apr 6 '19 at 16:04
  • $\begingroup$ @YuvalFilmus I'm not sure it matters, since this question is purely about a feature of the C language, which is off-topic, here. But I agree this should be clarified when the question is asked on Stack Overflow. $\endgroup$ – David Richerby Apr 6 '19 at 16:09
  • $\begingroup$ I mean one is integer variable and another is integer pointer. Both integer type . $\endgroup$ – Srestha Apr 6 '19 at 16:15
  • 2
    $\begingroup$ They're not the same type. One is an integer, another is a pointer, i.e., a memory address. See en.wikipedia.org/wiki/Pointer_(computer_programming). $\endgroup$ – Yuval Filmus Apr 6 '19 at 16:22
  • $\begingroup$ @DavidRicherby This is a rather general question, which has little to do with C. It is about the difference between pointers and what they point at. $\endgroup$ – Yuval Filmus Apr 6 '19 at 16:23
1
$\begingroup$

An integer is a value, whereas a pointer to an integer is a pointer, that is, a memory address. In most systems, all pointers have the same size, but the objects they point to can have different sizes. For example, in your systems pointers are 8 bytes long, whereas integers come in various sizes — possibly from 1 byte up to 8 bytes.

You might be misled by the specific quirky syntax of C, which allows declarations of the form

int a, *b;

This declares an integer a and a pointer b. This is just a confusing feature of C syntax.

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ "whereas integers come in various sizes — possibly from 1 byte up to 8 bytes. "-Can you tell me how? $\endgroup$ – Srestha Apr 6 '19 at 19:51
  • $\begingroup$ This is already getting to be specific to the C programming language. C has several integer datatypes, which generally have different sizes, though the exact sizes are implementation-dependent. Any good text on C would cover these. $\endgroup$ – Yuval Filmus Apr 6 '19 at 19:53
  • $\begingroup$ You mean size of integer can be 2B or 4B or 8B in 64bit system? Then pointer size also can vary, right? $\endgroup$ – Srestha Apr 6 '19 at 19:59
  • 1
    $\begingroup$ On modern systems pointer sizes don't vary. There's certainly no reason for them to vary depending on what they are pointing at. See stackoverflow for the situation in the past. The available integer sizes depend on the system. See Wikipedia. $\endgroup$ – Yuval Filmus Apr 6 '19 at 20:03
  • $\begingroup$ Sizeof(int) is entirely up to the C compiler except it must be >= 1. It could be 42. Which would be unusual, but entirely possible. $\endgroup$ – gnasher729 Apr 7 '19 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.