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
5
  • 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. Commented May 17, 2015 at 8:01
  • stackoverflow.com/questions/37538/… Commented May 17, 2015 at 8:02
  • and stackoverflow.com/questions/492384/… Commented May 17, 2015 at 8: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. Commented May 17, 2015 at 8: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). Commented May 17, 2015 at 8:13

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.