[英]s expects argument of type char c but argument 2 has type 'int' warning and bad return
Yes ,I know that this question was already asked for many times ,but none of these helped me to discover the problem (duplicate...yeah). 是的,我知道这个问题已经被问过很多次了,但是这些都没有帮助我发现问题(重复...是的)。 I want to read from input a series of strings into an array and then search from 'First Name'.
我想从输入中读取一系列字符串到数组中,然后从“名字”中搜索。 If the name exist ,I want to display all the data stored in that element of array (I attached the code to undestand easily).
如果名称存在,我想显示存储在该数组元素中的所有数据(我附加了代码以使其不容易理解)。 When I run it ,I read from keyboard all the data ,but it returns me absolutely nothing.
当我运行它时,我从键盘上读取了所有数据,但是绝对没有返回任何信息。
#include<stdio.h>
typedef struct record {
char name[10],lname[10],phone[10],bday[10];
};
void main() {
struct record rec;
char search;
int i,nr;
printf("\nInput number of records: ");
scanf("%d",&nr);
for (i=0 ; i<nr ;i++) {
printf("First name: ");
scanf("%s",&rec.name[i]);
printf("Last name: ");
scanf("%s",&rec.lname[i]);
printf("Phone: ");
scanf("%s",&rec.phone[i]);
printf("Bday: ");
scanf("%s",&rec.bday[i]);
}
printf("Input the first name for searching: ");
scanf("%s",&search);
for (i=0 ;i<nr;i++) {
if (search == rec.name[i]) {
printf("First name: %s\nLast name: %s\nPhone: %s\nB-day: %s",rec.name[i],rec.lname[i],rec.phone[i],rec.bday[i]);
}
}
}
NOTE: I already replaced 注意:我已经更换
scanf("%s",&rec.name[i]);
with 同
scanf("%s",rec.name[i]);
but no effect. 但没有效果。
I believe there are a lot of problems with your code. 我相信您的代码有很多问题。
Firstly in this line: 首先在这一行:
scanf("%s",&search);
You have declared search
as only a char
, when really you want an array of chars. 当您确实需要一个
char
数组时,您已经声明search
仅是一个char
。 You also don't need &
with search
, as an array decays to a pointer to the first element. 您也不需要
&
search
,因为数组会衰减为指向第一个元素的指针。
It instead should be like this: 相反,它应该是这样的:
char search[10];
scanf("%9s", search); /* %9s to avoid buffer overflow */
You need to make this change to all your other scanf()
calls, as this seems to be everywhere in this code. 您需要对所有其他
scanf()
调用进行此更改,因为在此代码中似乎无处不在。
It also seems that you want to create an array of records(structures), So you might need to make this after getting the value of nr
. 似乎还想创建一个记录(结构)数组,因此您可能需要在获取
nr
的值之后进行此操作。 You can create it like this: 您可以这样创建它:
struct record rec[nr]; /* array of nr structures */
This also means calls like this: 这也意味着像这样的调用:
rec.name[i]
Don't make sense, as you are iterating over the characters within a name, not over all the records in struct records
. 没什么意义,因为您要遍历名称中的字符,而不是遍历
struct records
所有struct records
。
This needs to be instead: 应该改为:
rec[i].name
Secondly, Your using ==
to compare strings, when you should be using strcmp
instead. 其次,当应该使用
strcmp
时,使用==
比较字符串。 Using ==
will only compare the base address of the strings, not the actual contents of strings. 使用
==
将仅比较字符串的基地址,而不是字符串的实际内容。
Your line should be this instead: 您的行应改为:
if (strcmp(search, rec[i].name) == 0) {
If you read the manual page for strcmp()
, checking for a return value of 0
means that both strings are equal in comparison. 如果您阅读了
strcmp()
的手册页,则检查返回值0
意味着两个字符串在比较中相等。
Lastly, in your first scanf()
call: 最后,在您的第一个
scanf()
调用中:
scanf("%d",&nr);
You should really check the return value of this: 您应该真正检查以下内容的返回值:
if (scanf("%d", &nr) != 1) {
/* exit program */
}
Note: For reading strings, you should really be using fgets
instead. 注意:要读取字符串,您实际上应该使用
fgets
。 You can try upgrading to this later, but I think it is better to understand these basics first. 您可以稍后再尝试升级到此版本,但我认为最好先了解这些基础知识。
#include <stdio.h>
#include <string.h>
#define STRSIZE 10
typedef struct {
char name[STRSIZE+1]; /* +1 to account for null-btye at the end */
char lname[STRSIZE+1];
char phone[STRSIZE+1];
char bday[STRSIZE+1];
} record;
int main() {
char search[STRSIZE+1];
int i,nr;
printf("\nInput number of records: ");
if (scanf("%d", &nr) != 1) {
printf("Invalid input.\n");
return 1;
}
record rec[nr]; /* array of records */
for (i = 0; i < nr ; i++) {
printf("First name: ");
scanf("%10s", rec[i].name);
printf("Last name: ");
scanf("%10s", rec[i].lname);
printf("Phone: ");
scanf("%10s", rec[i].phone);
printf("Bday: ");
scanf("%10s", rec[i].bday);
}
printf("Input the first name for searching: ");
scanf("%10s", search);
for (i = 0; i < nr; i++) {
if (strcmp(search, rec[i].name) == 0) {
printf("First name: %s\nLast name: %s\nPhone: %s\nB-day: %s\n",rec[i].name,rec[i].lname,rec[i].phone,rec[i].bday);
} else {
printf("Record not found.\n");
}
}
return 0;
}
The numeric input leaves a new line character in the input buffer, which is then picked up by the character input. 数字输入在输入缓冲区中留下一个换行符,然后由字符输入拾取。 when numeric input with
scanf()
skips leading white space, character input does not skip this leading white space. 当用
scanf()
输入的数字跳过前导空格时,字符输入不会跳过此前导空格。 Use a space before %c
and it will help you cause if space is not used then a buffer added with value .so that use space before %c
在
%c
之前使用空格,这将有助于您解决问题,如果不使用空格,则添加一个带有值.buffer的缓冲区,以便在%c
之前使用空格
scanf(" %c",&rec.name[i]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.