Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 警告:格式“%d”要求类型为“int”,但参数2的类型为“int(*)(int*)”_C_Arrays - Fatal编程技术网

C 警告:格式“%d”要求类型为“int”,但参数2的类型为“int(*)(int*)”

C 警告:格式“%d”要求类型为“int”,但参数2的类型为“int(*)(int*)”,c,arrays,C,Arrays,我尝试创建一个程序,主要是声明一个数组并加载它的元素。 还有一个函数可以计算数组中的元素数,但是当我想显示结果时,会出现一个内存地址,而不是上面的元素数和警告 守则: #include <stdio.h> #include <conio.h> int countArrayElement(int arr[]); int main() { int intMyArray[]= {1,2,3,4,6,7}; countArrayElement(intMyArr

我尝试创建一个程序,主要是声明一个数组并加载它的元素。 还有一个函数可以计算数组中的元素数,但是当我想显示结果时,会出现一个内存地址,而不是上面的元素数和警告

守则:

#include <stdio.h>
#include <conio.h>

int countArrayElement(int arr[]);

int main()
{
    int intMyArray[]= {1,2,3,4,6,7};
    countArrayElement(intMyArray);
    printf("The quantity of elements in the array is %d",&countArrayElement);
    getch();
    return 0;
}

int countArrayElement(int arr[])
{
    int count = 0;
    count  = sizeof (arr)/sizeof(int);  // wont work at all, arr is just the first element
    return count;
}

错误消息说明了一切。在格式字符串中,%d表示整数值,而您给它一个指向函数的指针。您希望将print to参数更改为函数调用的实际结果:

printf("The quantity of elements in the array is %d", countArrayElement(intsMyArray));

在C语言中检索数组中的元素并不像其他语言那样简单。

PrintF数组中的元素数量为%d,countArrayElement;不要使用建议更正请注意,函数中的sizeof arr将给出int*的大小,因为int arr[]将解析为int*的大小,而不是数组的大小。sizeof是一个编译时操作…除了可变长度数组之外,如chux在下面指出的,但这不会改变任何事情——在函数中,你不能用这种方式确定数组的大小operand@AlejandroCaro我更新了帖子-在print调用的参数中不包含与号。执行指定的1@AlejandroCaro你看过我链接的帖子了吗?@AlejandroCaro:见我上面的评论。这是意料之中的事。将int[]作为参数赋予函数只是赋予int*的一种奇特方式。考虑:您不想传递,因此,复制整个数组。。。此外,sizeof是一个编译时操作符,而不是一个运行时函数!