Why isn't the size of an array parameter the s

2018-12-31 00:23发布

Why isn't the size of an array sent as a parameter the same as within main?

#include <stdio.h>

void PrintSize(int p_someArray[10]);

int main () {
    int myArray[10];
    printf("%d\n", sizeof(myArray)); /* As expected, 40 */
    PrintSize(myArray);/* Prints 4, not 40 */
}

void PrintSize(int p_someArray[10]){
    printf("%d\n", sizeof(p_someArray));
}

13条回答
浪荡孟婆
2楼-- · 2018-12-31 00:41

In the C language, there is no method to determine the size of an unknown array, so the quantity needs to be passed as well as a pointer to the first element.

查看更多
看淡一切
3楼-- · 2018-12-31 00:43

In C language when you pass the array as an argument to the function , it is automatically converted into pointer ,array passing from one function other function is know as call by reference . That is the reason the called function only receives the pointer which point to the first element of function This is the reason

fun(int a[]) is similar to fun(int *a) ;

so when you print the size of array it will print the size of first element.

查看更多
爱死公子算了
4楼-- · 2018-12-31 00:46

So, you will need to pass the lenght of the array as a second parameter. When you are writing code, in which you both declare an array of constant size, and later pass that array to a function, it is a pain to have the array-length constant show up several places in your code...

K&R to the rescue:

#define N_ELEMENTS(array) (sizeof(array)/sizeof((array)[0])) 

So now you can do e.g:

int a[10];
...
myfunction(a, N_ELEMENTS(a));
查看更多
萌妹纸的霸气范
5楼-- · 2018-12-31 00:47

You can't pass arrays to functions.

If you really wanted to print the size, you could pass a pointer to an array, but it won't be generic at all as you need to define the array size for the function as well.

#include <stdio.h>

void PrintSize(int (*p_anArray)[10]);

int main(void) {
    int myArray[10];
    printf("%d\n", sizeof(myArray)); /* as expected 40 */
    PrintSize(&myArray);/* prints 40 */
}

void PrintSize(int (*p_anArray)[10]){
    printf("%d\n", (int) sizeof(*p_anArray));
}
查看更多
心情的温度
6楼-- · 2018-12-31 00:49

In 'C' programming languange 'sizeof()' is the operator and he returns the size of the object in bytes.Argument of the 'sizeof()' operator must be a left-value type(integer,float number,struct,array).So if you want to know the size of an array in bytes you can do it very simple.Just use the 'sizeof()' operator and for his argument use the array name.For example:

#include <stdio.h>

main(){

 int n[10];
 printf("Size of n is: %d \n", sizeof(n)); 

}

Output on 32 bit system will be: Size of n is: 40.Because ineteger on 32 system is 4bytes.On 64x it is 8bytes.In this case we have 10 integers declared in one array.So the result is '10 * sizeof(int)'.

Some tips:

If we have an array declared like this one 'int n[]={1, 2, 3, ...155..};'. So we want to know how many elements are stored in this array. Use this alghorithm:

sizeof(name_of_the_array) / sizeof(array_type)

Code: #include

main(){

int n[] = { 1, 2, 3, 44, 6, 7 };
printf("Number of elements: %d \n", sizeof(n) / sizeof(int));
return 0;

}

查看更多
柔情千种
7楼-- · 2018-12-31 00:52

The behavior is by design.

Same syntax in function parameter declaration means completely different thing than in local variable definition.

The reason is described in other answers.

查看更多
登录 后发表回答