0

In C, array elements are treated as pointers. So the expression sizeof(arr)/sizeof(arr[0]) becomes sizeof(int *)/sizeof(int) which results in 1 then why the following program give output is 12 11.

#include <stdio.h>

    int main()
    {
            int n1, n2;
            char arr1[] = "Hello World";
            char arr2[] = {'H','e','l','l','o',' ','W','o','r','l','d'};
            n1 = sizeof(arr1)/sizeof(arr1[0]);
            n2 = sizeof(arr2)/sizeof(arr2[0]);
            printf("n1 = %d\nn2 = %d\n", n1, n2);
            return 0;
    }
6
  • 5
    "In C, array elements are treated as pointers. So the expression sizeof(arr)/sizeof(arr[0]) becomes sizeof(int *)/sizeof(int)" where did you learn these falsehoods? Commented Sep 14, 2016 at 12:42
  • @Kninnug please see : geeksforgeeks.org/why-c-treats-array-parameters-as-pointers Commented Sep 14, 2016 at 12:44
  • 3
    @Anjan: sizeof is operator, not a function. Commented Sep 14, 2016 at 12:45
  • 2
    @Anjan, that page talks about array parameters, which your code doesn't have. Commented Sep 14, 2016 at 12:45
  • 1
    arr1 has null-terminator(\0). Commented Sep 14, 2016 at 12:53

3 Answers 3

2

array elements are treated as pointers

It is not a true. Arrays are NOT pointers. But they decay to pointers to first element when are passed to functions. And sizeof(array) is the number of bytes that whole array occupies.

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

Comments

0

In C, array elements are treated as pointers.

False . Array decays to pointer when passed to function . But you dont have any function to which array is being passed as parameter (totally unrelated stuff).

sizeof operator is one of exceptions. sizeof will give you the size array occupies in the memory.

Comments

0

To make the Serhio's answer clearer it's important to note sizeof() is an operator and not a function as said before by Grzegorz on his comment. It means its evaluantion is made in compile-time, but what does it means?

Once arr1 or arr2 are declared in that way the compiler knows its size before execution starts and than sizeof(arr1) will be converted to sizeof(char[12]) (it includes \0 char at the end) and sizeof(arr2) to sizeof(char[11]), which results in 12 and 11.

As it's made in compile-time the compiler knows they must be handled as arrays and not pointers by the sizeof() operator, what is completely different to dynamically allocated arrays, which are allocated in execution-time and sizeof() will operates over a normal char pointer size (architecture dependent: it may be 4 bytes on 32bits systems or 8 bytes on 64bits).

6 Comments

With int n; scanf("%d", &n); int a[n]; printf("%zu\n", sizeof a);, the size of a is not computed at compile time but at run time. The result is not necessarily 1.
@chux what I meant by dinamically allocated was using malloc(), and than dividing the result by sizeof(<element_type>), as @Anjan pointed on his question, but indeed the result will be 1 just if element_type is int, sorry, I'll fix it. And in your code you need to be careful: if you swap scanf() call with the a[n] declaration the sizeof() will compile but will result in wrong values (0 for example). When I need something like that I prefer to use malloc(). I prefer the recommendations from C89 about declaring every variable at the beginning, before any function calls.
int a[n] is dynamically allocated array and is a counter example to this answer. The array is declared, yet the compiler does not know it size. The result of sizeof a is determined at run-time - not compile time. The result is the size of the array, not the size of a pointer.
@chux I think you are using the "dynamic allocation" concept as "run-time", aren't you? As far as I know they are different things. int a[n] is an "automatic" array and not "dynamic", for instance, when you use int a[n] the stack will holds its value and not the heap and a can't be reallocated anymore. There are other differences yet, take a look at: GCC and here. So yor example isn't a counter example of my explanation about "dynamic allocated arrays".
"when you use int a[n] the stack will holds its value and not the heap" --> maybe, maybe not. C does not specify that nor stack nor heap - those are unspecified implementation details. C does not specify "dynamically allocated". This differs from your GCC reference. If we limit ourselves to GCC world, your answer is better defined. My comments reflect C in general.
|

Your Answer

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