The size of arrays and pointers

Here is my sample code

#include<stdio.h> void main() { int arr[]={1,2,3,4,5,6}; char *ptr,a; a='c'; ptr=&a; int *ptr1,a1; a1=4; ptr1=&a1; printf("%d %d %d",sizeof(arr), sizeof(ptr1), sizeof(ptr)); } 

Now, as I understand it, the size will indicate the size needed to store the variable, now for this there will be

24 4 4

Why is the size arr=24 , because it is just a pointer, and it should have size = 4?

Thanks.

+2
source share
5 answers

"... it's just a pointer ..."? No. An array is not a pointer. An array is an array object: a continuous continuous block of memory in which the elements of the array are stored, without any pointers. In your case, the array has 6 elements in size 4 each. This is why your sizeof is rated to 24.

A common misconception regarding pointer arrays has been debunked millions of times, but for some reason it appears all the time. Read the FAQ, come back if you have any questions about this.

http://c-faq.com/aryptr/index.html

PS As Jojim Pileborg correctly noted in his answer, sizeof not a function. This is the operator.


Another context in which arrays behave differently than pointers is the unary operator & ("operator address"). When unary & is applied to an int * pointer, an int ** pointer is created. When unary & is applied to an array of type int [10] , a pointer of type int (*)[10] . These are two different types.

 int *p = 0; int a[10] = { 0 }; int **p1 = &p; /* OK */ int **p2 = &a; /* ERROR */ int (*p3)[10] = &a; /* OK */ 

This is another popular source of questions (and errors): sometimes people expect & create an int ** pointer when applied to an int [10] array int [10] .

+5
source

When you have an array, sizeof returns the size of the array (note that this is the size in bytes of the array, not the number of elements in the array), otherwise it returns the size of the type or expression.

However, if you pass an array to a function, the array degrades to a pointer, and size information is lost.

Furthermore, technically speaking, sizeof not a function, it is a special keyword in the language and can be used without parentheses, as well as when used with expressions.

0
source

Arrays do not break into pointers inside sizeof ; sizeof(arr) returns the total size allocated for the array, which in this case is 6 * sizeof(int) .

0
source

From Wikipedia

When sizeof is applied to an array name, the result is the size in bytes of the entire array. (This is one of the few exceptions to the rule that the array name is converted to a pointer to the first element of the array.)

0
source

the sizeof() operator does not give you the number of elements in the array, it gives you the number of bytes the thing occupies in memory. Hence:

 #include <stdio.h> int main() { char s[] = { 1, 2, 3, 4, 5, 0 }; int xs[] = { 1, 2, 3, 4, 5, 0 }; printf( "sizeof( s ) = %d\n", sizeof( s ) ); printf( "sizeof( xs ) = %d\n", sizeof( xs ) ); return 0; } sizeof( s ) = 6 sizeof( xs ) = 24 
0
source

Source: https://habr.com/ru/post/1382680/


All Articles