简体   繁体   English

传递参数 1 从指针生成整数而没有强制转换警告

[英]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 toSetPerson更改为

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:如果要存储所有五个整数,则应用forwhile循环来扫描每个数字:

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.对于我在括号内写的条件,您必须应用whilefor循环来分别打印这些整数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告:传递&#39;memcpy&#39;的参数1会使指针从整数开始而无需强制转换 - warning: passing argument 1 of ‘memcpy’ makes pointer from integer without a cast 警告传递&#39;finder&#39;的参数2使指针从整数开始而无需强制转换 - Warning passing argument 2 of 'finder' makes pointer from integer without a cast 警告:传递&#39;fopen&#39;的参数2会使指针从整数开始而无需强制转换 - warning: passing argument 2 of ‘fopen’ makes pointer from integer without a cast 警告:传递&#39;calcFx&#39;的参数2将使指针产生整数而不进行强制转换 - warning: passing argument 2 of 'calcFx' makes integer from pointer without a cast 警告:传递&#39;strcpy&#39;的参数1会使指针从整数开始而不进行强制转换 - warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast 传递参数 1 从指针生成 integer 而没有对 putchar 的强制转换警告 - Passing Argument 1 makes integer from pointer without a cast warning for putchar 警告:传递“ quicksort”的参数1会使指针从整数开始而无需强制转换 - warning: passing argument 1 of 'quicksort' makes pointer from integer without a cast 警告:传递&#39;strcpy&#39;的参数2使得整数指针没有强制转换[-Wint-conversion] | - warning: passing argument 2 of 'strcpy' makes pointer from integer without a cast [-Wint-conversion]| 警告:传递&#39;memcpy&#39;的参数2使得指针来自整数而没有强制转换[默认启用] - warning: passing argument 2 of ‘memcpy’ makes pointer from integer without a cast [enabled by default] 为什么程序显示警告传递'circularswap'的参数1使指针来自integer而不进行强制转换? - Why does the program display warning passing argument 1 of 'circularswap' makes pointer from integer without a cast?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM