1

Example

#include <stdio.h>

struct A {
  char *b;
};

int main(int argc, char *argv[]) {
  char c[4] = { 'c', 'a', 't', '\0' };
  struct A a;
  a.b = c;
  printf("%s\n", a.b); // cat
  printf("%lu\n", sizeof c); // 4
  printf("%lu\n", sizeof a.b); // 8 ???
}

Why does sizeof a.b returns 8 and not 4? If I understood correctly, a.b returns the value that was assigned to it, which is c. But shouldn't it return the size of c (which is 4) then?

ave
  • 18,083
  • 9
  • 30
  • 39

5 Answers5

3

sizeof() operator gives the number of bytes allocated to the object and in your case the object is a pointer whose size looks like is 8 bytes on your system.

Gopi
  • 19,784
  • 4
  • 24
  • 36
2

You're calling sizeof() on two different types.

  • sizeof(a.b) is sizeof(char *), which is 8 on your platform.
  • sizeof(c) is sizeof(char[4]), which is 4.

We can have pointers point to arrays via array decaying, which you can read about in this other answer: What is array decaying?

Community
  • 1
  • 1
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
1

First of all sizeof(a.b) is not size of c. It doesn't give size of what it is pointing to, rather it is size of the pointer.

Take an example of char:

  • size of char a is 1
  • and char *b is 4. (on 64 bit)

So it is size of the pointer not what it points to. Please note these sizes are platform dependent.

Although don't get confused by int. An int and int * are of same size on some platforms.

Anil Bhaskar
  • 3,718
  • 4
  • 33
  • 51
  • Sizeof is platform dependent. Please read the question and the other answers check your `sizeof(char *)` and `sizeof(int *)` – JJoao Mar 12 '15 at 17:20
  • @JJao that is what i mentioned. My point is not about what exactly the size is, point is size of a pointer is not size of pointer object not what it points to. I would suggest you read my answer please. – Anil Bhaskar Mar 12 '15 at 17:24
  • Sorry to insist: Your basic reasoning is correct but some "facts" are not: from the question we can deduce that in this platform size of (char * ) is 8. In the same direction it is possible that size of int may be different form `sizeof (int*)` – JJoao Mar 12 '15 at 17:32
1

If I understood correctly, a.b returns the value that was assigned to it,

Not exactly. a.b is what's called an lvalue. This means that it designates a memory location. However it does not read that memory location yet; that will only happen if we use a.b within a larger context that expects the memory location to be read.

For example:

a.b = something;    // does not read a.b
something = a.b;    // does read a.b

The case of sizeof is one context where it does not read the memory location. In fact it tells you how many bytes comprise that memory location; it doesn't tell you anything about what is stored there (let alone about some other memory location that might be pointed to by what is stored there, if it is a pointer).

The output is telling you that your system uses 8 bytes to store a pointer.

M.M
  • 138,810
  • 21
  • 208
  • 365
-1

sizeof() returns the number of bytes of a variable. In this case sizeof ( char * ) returns 8 bytes which is the number of bytes that compose a pointer.

Francesco
  • 79
  • 1
  • 4