[英]Passing Argument 1 makes integer from pointer without a cast warning
I am using CodeBlocks and learning C. I created this simple script as a point for learning functions.我正在使用 CodeBlocks 并学习 C。我创建了这个简单的脚本作为学习函数的一个点。 I am not understanding the error I am getting though as everything matches up in my eyes.
我不明白我得到的错误,因为我眼中的一切都匹配。
CODE:代码:
#include <stdio.h>
#include <string.h>
void SetPerson(char a, int b);
int main () {
char name[50];
int number[6];
printf("Enter Name: ");
scanf("%49s", name);
printf("Enter Number: ");
scanf("%5d", number);
SetPerson(name, number);
return(0);
}
void SetPerson(char a, int b) {
printf("Name: %s\n", a);
printf("Number: %d", b);
}
In the compiler I am getting these errors:在编译器中,我收到这些错误:
||=== Build: Debug in remove (compiler: GNU GCC Compiler) ===|
C:\Users\e\Desktop\c programs\remove\main.c||In function 'main':|
C:\Users\e\Desktop\c programs\remove\main.c|17|warning: passing argument 1 of 'SetPerson' makes integer from pointer without a cast [-Wint-conversion]|
C:\Users\e\Desktop\c programs\remove\main.c|4|note: expected 'char' but argument is of type 'char *'|
C:\Users\e\Desktop\c programs\remove\main.c|17|warning: passing argument 2 of 'SetPerson' makes integer from pointer without a cast [-Wint-conversion]|
C:\Users\e\Desktop\c programs\remove\main.c|4|note: expected 'int' but argument is of type 'int *'|
C:\Users\e\Desktop\c programs\remove\main.c||In function 'SetPerson':|
C:\Users\e\Desktop\c programs\remove\main.c|23|warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat=]|
||=== Build finished: 0 error(s), 3 warning(s) (0 minute(s), 0 second(s)) ===|
EDIT:编辑:
I changed as recommend:我按照建议进行了更改:
SetPerson(char *a, int b);
And now I am at these errors:现在我遇到了这些错误:
||=== Build: Debug in remove (compiler: GNU GCC Compiler) ===|
C:\Users\e\Desktop\c programs\remove\main.c||In function 'main':|
C:\Users\e\Desktop\c programs\remove\main.c|17|warning: passing argument 2 of 'SetPerson' makes integer from pointer without a cast [-Wint-conversion]|
C:\Users\e\Desktop\c programs\remove\main.c|4|note: expected 'int' but argument is of type 'int *'|
||=== Build finished: 0 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
name
is an array of characters, but the first argument to SetPerson
is a single character. name
是一个字符数组,但SetPerson
的第一个参数是单个字符。 Change SetPerson
to将
SetPerson
更改为
void SetPerson(char* a, int b) {
printf("Name: %s\n", a);
printf("Number: %d", b);
}
Note that in C, arrays and pointers are basically interchangeable.请注意,在 C 中,数组和指针基本上是可以互换的。
Edit (after question was modified)编辑(修改问题后)
You're basically doing the same thing in reverse with the number.你基本上是在用数字相反的方式做同样的事情。 You have an array of ints (basically
int*
which you're passing to a int
argument.您有一个 int 数组(基本上是
int*
,您将其传递给int
参数。
Drop the array portion of the declaration for number
;删除
number
声明的数组部分; you're not telling the compiler to allocate a 6-digit (in base 10) number, you're telling it to allocate 6 32-bit (probably; int
is generally 32-bit these days, but may not be) numbers.你不是告诉编译器分配一个 6 位(以 10 为基数)的数字,而是告诉它分配 6 个 32 位(可能;现在
int
通常是 32 位,但可能不是)数字。
Then you'll need to change the arguments to scanf to pass a pointer to number
instead of the actual number;然后,您需要更改 scanf 的参数以传递指向
number
而不是实际数字的指针; use the address-of operator ( &
) for that:为此使用地址运算符 (
&
):
scanf("%d", &number);
Why are you using an array for an integer if you just want to store a single integer?如果您只想存储单个整数,为什么要使用整数数组? If you want to store five integers then you're doing it incorrectly.
如果您想存储五个整数,那么您就做错了。 If you want to store all five integers, then apply a
for
or while
loop for scanning each number:如果要存储所有五个整数,则应用
for
或while
循环来扫描每个数字:
int number[5];
for(int i = 0; i < 5 ; i++)
{
scanf("%d", number[i]);
}
In your code, scanf("%5d", number);
在您的代码中,
scanf("%5d", number);
would just store any number up to five digits, not those 5 digits separately.只会存储最多五位数字的任何数字,而不是分别存储这 5 位数字。
Now the thing left about passing those arrays to another functions.现在剩下的事情就是将这些数组传递给另一个函数。 Here you have to pass the addresses of those arrays as
SetPerson(name, number);
在这里,您必须将这些数组的地址作为
SetPerson(name, number);
传递SetPerson(name, number);
, and in the formal arguments you have to define pointers for the respective variables such as ,并且在形式参数中,您必须为相应的变量定义指针,例如
SetPerson( char *a, int *b )
Inside this function you're printing the string in a correct way, but not the integers (if you wanna print all the 5 integers in separately).在此函数中,您以正确的方式打印字符串,而不是整数(如果您想分别打印所有 5 个整数)。 For the condition I've written inside the parenthesis, you must apply a
while
or a for
loop to print those integers separately.对于我在括号内写的条件,您必须应用
while
或for
循环来分别打印这些整数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.