2

Note that if a is an array name, then sizeof(a) will yields the size of the entire array a and not the size of a pointer to one of its elements.

So for example, how does sizeof distinguish an array a and a pointer b?

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

int main(void) {
  int a[4] = {1, 2, 3, 4};
  int *b = a;
  printf("sizeof\(a) = %ld\n", sizeof(a));
  printf("sizeof\(b) = %ld\n", sizeof(b));
  return EXIT_SUCCESS;
}

It prints as below:

sizeof(a) = 16
sizeof(b) = 8
Andrey Chernukha
  • 21,004
  • 16
  • 95
  • 160
acgtyrant
  • 1,621
  • 1
  • 15
  • 24
  • 2
    Well, it's known at compile time what is an array and what is not, and since `sizeof` is evaluated at compile time too, it's not difficult to do that. – user4520 May 17 '15 at 08:01
  • http://stackoverflow.com/questions/37538/how-do-i-determine-the-size-of-my-array-in-c – c4f4t0r May 17 '15 at 08:02
  • and https://stackoverflow.com/questions/492384/how-to-find-the-sizeofa-pointer-pointing-to-an-array – WhozCraig May 17 '15 at 08:03
  • 2
    Compare what happens if you put the `printf` calls in a separate function and pass `a` and `b` as pointers, and you'll get a clear idea of how it works. – Theodoros Chatzigiannakis May 17 '15 at 08:05
  • 2
    The compiler does that, **not** the `sizeof` operator. How does it do it? Well, how do you do it? A simple glance is enough, right? It just as easy for the compiler (or whoever implements it). – barak manos May 17 '15 at 08:13

2 Answers2

8

sizeof is a compile-time operator. It is computed by the compiler (and is almost always a constant, VLAs being the exception).

The compiler is obviously knowing when a variable refers to an array or to a pointer because it has to know the type of every variable (and an array type is not the same as a pointer type).

Notice that in C an array can decay into a pointer (e.g. when you pass an array as an argument to a routine). This is one of the most tricky points of the C language (so dive into a C programming book if you don't understand it).

Basile Starynkevitch
  • 216,767
  • 17
  • 275
  • 509
1

the compiler keeps an eye on every name(array, integer, pointer, ....) you define in your code, it's address and it's size and save it in a table along with other inforamtion . so when you use the sizeof operator the compiler just replaces the expression with the size of that name and compile your program with a hard-coded number that is: the size of the operand, and that is why you can't use sizeof with dynamically sized structures at run time. So, in your example the compiler has a table like

name____address____size_____....(other things)

a_____1000_____16_____....(the array) <<<

b_____1016_____8_____.....

.._____1024_____.._____.......

and when you use an expression like

printf("sizeof\(a) = %ld\n", sizeof(a));

the compiler will replace it (at one of the translation phases) with

printf("sizeof\(a) = %ld\n", 16);

and then continue it's compilation work

the accountant
  • 496
  • 5
  • 14