Why sizeof(array) and sizeof(&array[0]) gives different results?
Asked Answered
D

8

12
#include <stdio.h>
int main(void){
    char array[20];

    printf( "\nSize of array is %d\n", sizeof(array) );  //outputs 20
    printf("\nSize of &array[0] is %d\n", sizeof(&array[0]); //output 4
}

Code above gives 20 for sizeof(array) and 4 for sizeof(&array[0]).

What I knew was instead of giving array as a argument, its first element can be passed. Shouldn't they give same output as 20? and why &array[0] gives 4 as result? char is stored in 1 byte as far as I know?

Deicide answered 26/6, 2013 at 12:37 Comment(2)
read this: What does sizeof(&arr) returns?Dogcart
note: sizeof(array)/sizeof(&array[0]) give you length == 20Dogcart
N
19

In the expression sizeof array, array is not converted to a pointer type.

C11, § 6.3.2.1 Lvalues, arrays, and function designators

Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue.

Therefore, its type is char[20], not char *. The size of this type is sizeof(char) * 20 = 20 bytes.

C11, § 6.5.3.4 The sizeof and _Alignof operators

The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand.

&array[0] type is char *. That's why the sizeof(&array[0]) gives the same result as sizeof(char *) (4 bytes on your machine).

Nagaland answered 26/6, 2013 at 12:39 Comment(0)
Z
10

The variable array is an array of 20 char, the value of the expression sizeof(array) is equal to the sizeof bytes of an array of 20 chars, which is 20.

&array[0] is a pointer. The sizeof returns the size of a pointer, which is 4 in your machine.

Zuckerman answered 26/6, 2013 at 12:39 Comment(0)
E
5

The expression &array[0] is a pointer so the sizeof operator returns the size of the pointer.

Educative answered 26/6, 2013 at 12:39 Comment(0)
R
2

sizeof(array) is giving you size of total array and sizeof(&array[0]) is giving you sizeof(char *)

Your question is perfect example of Array and pointer are not same. Array may act as pointer.Have a look here.

Reconstructive answered 26/6, 2013 at 12:39 Comment(0)
L
1

&array[0] returns a pointer to the first index of array. Using the sizeof operator on a pointer will yield the size of a pointer (depends on ABI), in your case, it's 4.

Lumpy answered 26/6, 2013 at 12:39 Comment(1)
"Using the sizeof operator on a pointer will always yield 4." - That's not true..Zuckerman
P
1

sizeof(&array[0]) is returning the size of a pointer to a char. array[0] yields a char, the & returns a pointer to the char. Your compiler used 4 bytes for a pointer (32 bit).

Parlin answered 26/6, 2013 at 12:41 Comment(0)
R
-1

The "array" itself is constant pointer, you can not address it to different location. Yes, array address is the same as &array[0], which is derived from &(array+0) and it is equal to &(array). We know that &* is eliminated so we are back to "array" again. So "array" is the same as "&*(array+0)" But in regards to sizeof operator, the operand must be in array notation, not in pointer notation. To clarify:

char *data = {"Today is Sunday"}; ... Pointer notation

char data[] = {"Today is Sunday"}; ... Array notation

In the first example with pointer notation the sizeof(data) will be the size of bytes required to store an address, which is 8 on my system. In the second example the sizeof(data) will give the number of bytes occupied by the array, which would be 15 chars in the string. But if you use pointer notation like sizeof(data+0) the result will be the number of bytes required to store an address, which is 8 on my system.

sizeof operator result is the same for pointer and for array with pointer notation.

Rybinsk answered 25/11, 2018 at 20:54 Comment(1)
Arrays are not pointers. With char *data = "something"; you have data, a pointer to char, yet with char data[] = "something"; you have data, an array of char. Arrays decay to pointers to their first elements in most expressions, but not when they are operands to sizeof. Pointer notation versus array notation has to do with array indexing, not pointer initialization (first example) or array initialization (second example).Plantaineater
I
-2

I think that "array" itself is constant pointer, you can not address it to different location. It is true that we can write: array is the same as &array[0], which is derived from &*(array+0) and it is equal to &*(array). We know that &* is eliminated so we are back to "array" again. So "array" is the same as "&*(array+0)" The terms should be the same in some circumstances, but it is not the same for the sizeof operator.

Indole answered 22/11, 2018 at 21:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.