【发布时间】:2015-10-28 21:57:36
【问题描述】:
我正在尝试将用户输入的变量“find”传递给此函数并返回用户输入的数字的下标位置(在现有数组中)。我看到了一些关于此的其他帖子,但无法真正理解所解释的内容。对不起,初学者。
它不是很完整,但由于一些我不确定的错误,我无法编译。
- 警告传递 'finder' 的参数 2 使指针从整数而不进行强制转换。它指向:
num_loc = finder(find, sort_num[10]);
这里我将“num_loc”设置为函数中“where”的返回
num_loc = finder(find, sort_num[10]);
printf( "\nYour number is located in memory location %d of the array",num_loc );
-
"[Note] 预期为 'int *' 但参数的类型为 'int'" 指向我的函数原型。
//fprototype outside the main at the beginning of the file int finder(int f,int x[]);
这是我的功能:
//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 x[]是一个int *(在此上下文中是一个指针或数组)。你用sort[10]调用它,这可能是一个int,所以警告是合乎逻辑的:你从一个整数值(你给的sort[10])创建一个指针(需要int*)。 -
question/printf() 语句中的“内存中的位置”似乎表示内存地址,但是,
%d用于打印整数。要打印地址,请使用:%p
标签: c compiler-errors