C 警告通过'的参数2;查找器';从不带强制转换的整数生成指针
我试图将用户输入的变量“find”传递到此函数中,并返回用户输入的数字(在现有数组中)的下标位置。我看到了其他一些关于这一点的帖子,但无法真正理解所解释的内容。对不起,初学者 它不是很完整,但我无法编译,因为有几个错误,我不确定C 警告通过'的参数2;查找器';从不带强制转换的整数生成指针,c,compiler-errors,C,Compiler Errors,我试图将用户输入的变量“find”传递到此函数中,并返回用户输入的数字(在现有数组中)的下标位置。我看到了其他一些关于这一点的帖子,但无法真正理解所解释的内容。对不起,初学者 它不是很完整,但我无法编译,因为有几个错误,我不确定 警告:传递“finder”的参数2会使指针从整数变为不带强制转换的整数。它指向: num_loc=finder(find,sort_num[10]) 这里我将“num_loc”设置为函数中“where”的返回 num_loc=finder(find,sort_num[
num_loc=finder(find,sort_num[10])代码>
这里我将“num_loc”设置为函数中“where”的返回
num_loc=finder(find,sort_num[10]);
printf(“\n您的号码位于数组的内存位置%d”,num\u loc)代码>
“[注意]应为'int*',但参数的类型为'int'”,它指向我的函数原型
//fprototype位于文件开头的main之外
int finder(intf,intx[])代码>
以下是我的功能:
//function located at the end of the file outside the main
int finder(int f, int x[])
{
int found = 0;
int where;
int i = 0;
while (found != 1){
if (x[i] == f){
found = 1;
where = i;
return where;
}
else{
++i;
}
}
}
相当于
int num = sort_num[10]; // Problem. Accessing array out of bounds.
num_loc = finder(find, num); // Problem. Using an `int` when an `int*` is expected.
// That's what the compiler is complaining about.
在调用finder
时,只需使用sort_num
num_loc = finder(find, sort_num);
真正的解决方案是更改finder
以接受另一个参数,该参数指示sort\u num
中的元素数。否则,您将面临访问数组越界的风险。它也可以简化很多
int finder(int f, int x[], int arraySize)
{
for ( int i = 0; i < arraySize; ++i )
{
if (x[i] == f)
{
return i;
}
}
// Not found
return -1;
}
相当于
int num = sort_num[10]; // Problem. Accessing array out of bounds.
num_loc = finder(find, num); // Problem. Using an `int` when an `int*` is expected.
// That's what the compiler is complaining about.
在调用finder
时,只需使用sort_num
num_loc = finder(find, sort_num);
真正的解决方案是更改finder
以接受另一个参数,该参数指示sort\u num
中的元素数。否则,您将面临访问数组越界的风险。它也可以简化很多
int finder(int f, int x[], int arraySize)
{
for ( int i = 0; i < arraySize; ++i )
{
if (x[i] == f)
{
return i;
}
}
// Not found
return -1;
}
相当于
int num = sort_num[10]; // Problem. Accessing array out of bounds.
num_loc = finder(find, num); // Problem. Using an `int` when an `int*` is expected.
// That's what the compiler is complaining about.
在调用finder
时,只需使用sort_num
num_loc = finder(find, sort_num);
真正的解决方案是更改finder
以接受另一个参数,该参数指示sort\u num
中的元素数。否则,您将面临访问数组越界的风险。它也可以简化很多
int finder(int f, int x[], int arraySize)
{
for ( int i = 0; i < arraySize; ++i )
{
if (x[i] == f)
{
return i;
}
}
// Not found
return -1;
}
相当于
int num = sort_num[10]; // Problem. Accessing array out of bounds.
num_loc = finder(find, num); // Problem. Using an `int` when an `int*` is expected.
// That's what the compiler is complaining about.
在调用finder
时,只需使用sort_num
num_loc = finder(find, sort_num);
真正的解决方案是更改finder
以接受另一个参数,该参数指示sort\u num
中的元素数。否则,您将面临访问数组越界的风险。它也可以简化很多
int finder(int f, int x[], int arraySize)
{
for ( int i = 0; i < arraySize; ++i )
{
if (x[i] == f)
{
return i;
}
}
// Not found
return -1;
}
这是函数定义的第一部分:
intfinder(intf,intx[])
第二个参数是一个int指针,编译器会告诉您:
应为“int*”
您使用以下命令调用了函数:
num_loc=finder(find,sort_num[10])代码>
如果sort_num是一个整数数组,则sort_num[10]的计算结果为该数组中第11位的整数。所以你要传递给finder函数那个整数,而不是int指针。如果sort_num是一个整数数组,请将调用重写为:
num\u loc=finder(查找、排序\u num)代码>
这样,您将传递一个int指针,该指针保存sort_num数组中第一个元素的地址。这是函数定义的第一部分:
intfinder(intf,intx[])
第二个参数是一个int指针,编译器会告诉您:
应为“int*”
您使用以下命令调用了函数:
num_loc=finder(find,sort_num[10])代码>
如果sort_num是一个整数数组,则sort_num[10]的计算结果为该数组中第11位的整数。所以你要传递给finder函数那个整数,而不是int指针。如果sort_num是一个整数数组,请将调用重写为:
num\u loc=finder(查找、排序\u num)代码>
这样,您将传递一个int指针,该指针保存sort_num数组中第一个元素的地址。这是函数定义的第一部分:
intfinder(intf,intx[])
第二个参数是一个int指针,编译器会告诉您:
应为“int*”
您使用以下命令调用了函数:
num_loc=finder(find,sort_num[10])代码>
如果sort_num是一个整数数组,则sort_num[10]的计算结果为该数组中第11位的整数。所以你要传递给finder函数那个整数,而不是int指针。如果sort_num是一个整数数组,请将调用重写为:
num\u loc=finder(查找、排序\u num)代码>
这样,您将传递一个int指针,该指针保存sort_num数组中第一个元素的地址。这是函数定义的第一部分:
intfinder(intf,intx[])
第二个参数是一个int指针,编译器会告诉您:
应为“int*”
您使用以下命令调用了函数:
num_loc=finder(find,sort_num[10])代码>
如果sort_num是一个整数数组,则sort_num[10]的计算结果为该数组中第11位的整数。所以你要传递给finder函数那个整数,而不是int指针。如果sort_num是一个整数数组,请将调用重写为:
num\u loc=finder(查找、排序\u num)代码>
这样,您将传递一个int指针,该指针保存sort_num数组中第一个元素的地址。我觉得很愚蠢。。。。就这些。。谢谢。int x[]
是一个int*
(在此上下文中是指针或数组)。您使用sort[10]
调用它,这可能是一个int
,因此警告是合乎逻辑的:您从一个整数值(您给出的sort[10]
)生成一个指针(所需的int*
)