在作为指针传递的结构中使用 strcmp()
Using strcmp() in structures passed as pointers
我有一堆关于结构的练习题,所有都涉及作为函数参数指针传递的结构。现在,我手头有一个具体问题,它要求我在 phone 书籍结构中存储一些姓名和电话 phone 号码。内容如下:
Write a C program that implements the following two functions. The function readin()
reads a number of
persons’ names and their corresponding telephone numbers, passes the data to the caller
via the parameter p
, and returns the number of names that have entered. The character #
is used
to indicate the end of user input. The function search()
finds the telephone number of an input name
target, and then prints the name and telephone number on the screen. If the input name cannot be
found, then it will print an appropriate error message. The prototypes of the two functions are given below:
int readin(PhoneBk *p);
void search(PhoneBk *p, int size, char *target);
PhoneBk
的结构定义如下:
typedef struct {
char name[20];
char telno[20];
} PhoneBk;
节目模板如下:
#include <stdio.h>
#include <string.h>
#define MAX 100
typedef struct {
char name[20];
char telno[20];
} PhoneBk;
int readin(PhoneBk *p);
void search(PhoneBk *p, int size, char *target);
int main() {
PhoneBk s[MAX];
char t[20];
int size;
size = readin(s);
printf("Enter search name: \n");
gets(t);
search(s, size, t);
return 0;
}
int readin(PhoneBk *p) {
/* Write your program code here */
}
void search(PhoneBk *p, int size, char *target) {
/* Write your program code here */
}
我必须在我的程序中比较字符串两次:第一次,检查是否输入了 #
作为名称(在这种情况下,我希望程序直接跳转到搜索功能而无需询问 phone 号码);第二,如果目标名称与 phone 一本书结构中的名称相匹配。
现在我完全不理解结构被声明为数组但作为指针传递的概念,所以我认为获取下一条记录的用户输入的方法是递增指针变量(此处 p
)。对于 get-the-records-until-#-is-entered 函数,我写了这个(使用 strcmp
):
int readin(PhoneBk *p) {
strcpy(p->name, "Test"); //To guarantee a character that is not #.
while (strcmp(p->name, "#") != 0) {
printf("Enter person name, # to stop.\n");
scanf("%s", p->name);
printf("Enter the telephone number.\n");
scanf("%s", p->telno);
p++;
}
return p;
}
我的意图是将 p->name
类型转换为字符串,以便 strcmp()
可以处理它。但是反复显示警告:
warning: cast from pointer to integer of different size
warning: passing argument 1 of strcmp() makes pointer from integer without a cast
这个错误已经在论坛上讨论过很多次(我搜索过),但我所看到的对这种结构和 strcmp()
.
的组合没有任何帮助
我完全放弃了strcmp()
第二个功能,就是找到目标名字显示人的记录,所以直接用循环比较字符串。
void search(PhoneBk *p, int size, char *target) {
int i, flag = 1, strCompare;
char c1 = *(p->name), c2 = *target, p1 = p->name, p2 = target;
while (c1 != '[=15=]' && c2 != '[=15=]') { //Comparing strings.
strCompare = (int)(c1 - c2);
if (strCompare != 0)
break;
else {
p1++;
p2++;
}
}
for (i = 0; i < size; i++, p++) {
if (strCompare == 0) {
flag = 0;
printf("Name = %s, Tel. = %s\n", p->name, p->telno);
}
if (flag == 1)
printf("Name not found.\n");
}
}
但我又收到了一些警告。
warning: cast from pointer to integer of different size
warning: initialization makes pointer from integer without a cast
我真的认为如果结构都作为数组传递,我的生活会更轻松,在这种情况下,我可以简单地使用 For 循环遍历结构数组并完成它。这是我在整个高中期间一直在做的事情,但现在我在大学里,我不得不处理作为指针传递的结构。
在这种情况下,我该如何处理 strcmp()
?如何将其应用于任何将结构作为指针传递的程序?
程序模板结构不合理,使用过时且危险的函数给出了一个非常糟糕的示例gets()
。
传递readin()
p
参数指向的数组中的结构数会好得多。从代码模板中,我们必须假设 MAX
结构可通过 p
.
访问
readin
函数应从标准输入读取最多 MAX
个条目,并使用索引或递增 p
将其存储到目标数组的相应元素中。
这是一个带有索引的解决方案:
int readin(PhoneBk *p) {
int i, n = MAX;
for (i = 0; i < n; i++) {
printf("Enter person name, # to stop.\n");
if (scanf("%19s", p[i].name) != 1 || p[i].name[0] == '#')
return i;
printf("Enter the telephone number.\n");
if (scanf("%19s", p[i].telno) != 1)
return i;
}
return i;
}
同样,搜索函数可以遍历从0
到size
的数组排除:
void search(PhoneBk *p, int size, char *target) {
int i, found = 0;
for (i = 0; i < size; i++) {
if (!strcmp(p[i].name, target) {
printf("Name = %s, Tel. = %s\n", p[i].name, p[i].telno);
found++;
}
}
if (found == 0)
printf("Name not found.\n");
}
关于警告,您在 strcmp((char)p->name, "#")
上有一个拼写错误,强制转换是不必要的,在这种情况下将指针转换为单个 char
,导致 2 个警告:一个用于将指针转换为一个不同大小的整数和另一个用于将整数传递给 strcmp()
而不是指向 char
.
的指针
在 readin 中,您不测试最后输入的名称,而是测试下一个条目的名称(未初始化),并且您也不检查是否到达 phone 书,也不是 EOF 案例。请注意,您还假设名称只是一个单词并且不能包含空格,但是因为 main 使用(丑陋的) gets 可能是组合名称是允许的。
为了不被多余的空格打扰,似乎需要删除它们。
例如:
#include <ctype.h>
void normalize(char * s)
{
/* remove unexpected 'isspace' */
int p1 = 0, p2 = 0;
while (s[p2] != 0) {
if (!isspace((unsigned char) s[p2]))
s[p1++] = s[p2];
else if ((p1 != 0) && (s[p1 - 1] != ' '))
s[p1++] = ' ';
p2 += 1;
}
s[((p1 != 0) && (s[p1 - 1] == ' ')) ? p1 - 1 : p1] = 0;
}
int readin(PhoneBk *p)
{
int n;
for (n = 0; n != MAX; ++n) {
puts("Enter person name, max length 18, # to stop.");
if (fgets(p->name, sizeof(p->name), stdin) == NULL) {
/* EOF */
return n;
}
normalize(p->name);
if (p->name[0] == '#')
break;
printf("Enter the telephone number, max length 18\n");
if (fgets(p->telno, sizeof(p->telno), stdin) == NULL) {
/* EOF, forget partial entry */
return n;
}
normalize(p->telno);
p += 1;
}
return n;
}
你的 search 太复杂了我宁愿不要试着去理解它,抱歉,定义可以很简单:
void search(PhoneBk *p, int size, char *target)
{
normalize(target);
while (size--) {
if (!strcmp(p->name, target)) {
printf("%s found, phone number is %s\n", target, p->telno);
return;
}
p += 1;
}
printf("unknown name %s\n", target);
}
注意我也对读名进行了规范化,以与本书的读法兼容。
如果我尝试不更改给定的 main 及其丑陋的 gets,编译和执行:
/tmp % gcc -g -pedantic -Wextra c.c
/tmp/ccdXFM1o.o: In function `main':
/tmp/c.c:17: warning: the `gets' function is dangerous and should not be used.
/tmp % ./a.out
Enter person name, max length 18, # to stop.
john do
Enter the telephone number, max length 18
12 34 56
Enter person name, max length 18, # to stop.
just me
Enter the telephone number, max length 18
1 2 3 4 5
Enter person name, max length 18, # to stop.
#
Enter search name:
john do
john do found, phone number is 12 34 56
/tmp %
/tmp % ./a.out
Enter person name, max length 18, # to stop.
john do
Enter the telephone number, max length 18
12 34 56
Enter person name, max length 18, # to stop.
just me
Enter the telephone number, max length 18
1 2 3 4 5
Enter person name, max length 18, # to stop.
#
Enter search name:
just me
just me found, phone number is 1 2 3 4 5
/tmp %
/tmp % ./a.out
Enter person name, max length 18, # to stop.
john do
Enter the telephone number, max length 18
12 34 56
Enter person name, max length 18, # to stop.
just me
Enter the telephone number, max length 18
1 2 3 4 5
Enter person name, max length 18, # to stop.
#
Enter search name:
me
unknown name me
我有一堆关于结构的练习题,所有都涉及作为函数参数指针传递的结构。现在,我手头有一个具体问题,它要求我在 phone 书籍结构中存储一些姓名和电话 phone 号码。内容如下:
Write a C program that implements the following two functions. The function
readin()
reads a number of persons’ names and their corresponding telephone numbers, passes the data to the caller via the parameterp
, and returns the number of names that have entered. The character#
is used to indicate the end of user input. The functionsearch()
finds the telephone number of an input name target, and then prints the name and telephone number on the screen. If the input name cannot be found, then it will print an appropriate error message. The prototypes of the two functions are given below:
int readin(PhoneBk *p);
void search(PhoneBk *p, int size, char *target);
PhoneBk
的结构定义如下:
typedef struct {
char name[20];
char telno[20];
} PhoneBk;
节目模板如下:
#include <stdio.h>
#include <string.h>
#define MAX 100
typedef struct {
char name[20];
char telno[20];
} PhoneBk;
int readin(PhoneBk *p);
void search(PhoneBk *p, int size, char *target);
int main() {
PhoneBk s[MAX];
char t[20];
int size;
size = readin(s);
printf("Enter search name: \n");
gets(t);
search(s, size, t);
return 0;
}
int readin(PhoneBk *p) {
/* Write your program code here */
}
void search(PhoneBk *p, int size, char *target) {
/* Write your program code here */
}
我必须在我的程序中比较字符串两次:第一次,检查是否输入了 #
作为名称(在这种情况下,我希望程序直接跳转到搜索功能而无需询问 phone 号码);第二,如果目标名称与 phone 一本书结构中的名称相匹配。
现在我完全不理解结构被声明为数组但作为指针传递的概念,所以我认为获取下一条记录的用户输入的方法是递增指针变量(此处 p
)。对于 get-the-records-until-#-is-entered 函数,我写了这个(使用 strcmp
):
int readin(PhoneBk *p) {
strcpy(p->name, "Test"); //To guarantee a character that is not #.
while (strcmp(p->name, "#") != 0) {
printf("Enter person name, # to stop.\n");
scanf("%s", p->name);
printf("Enter the telephone number.\n");
scanf("%s", p->telno);
p++;
}
return p;
}
我的意图是将 p->name
类型转换为字符串,以便 strcmp()
可以处理它。但是反复显示警告:
warning: cast from pointer to integer of different size
warning: passing argument 1 of strcmp() makes pointer from integer without a cast
这个错误已经在论坛上讨论过很多次(我搜索过),但我所看到的对这种结构和 strcmp()
.
我完全放弃了strcmp()
第二个功能,就是找到目标名字显示人的记录,所以直接用循环比较字符串。
void search(PhoneBk *p, int size, char *target) {
int i, flag = 1, strCompare;
char c1 = *(p->name), c2 = *target, p1 = p->name, p2 = target;
while (c1 != '[=15=]' && c2 != '[=15=]') { //Comparing strings.
strCompare = (int)(c1 - c2);
if (strCompare != 0)
break;
else {
p1++;
p2++;
}
}
for (i = 0; i < size; i++, p++) {
if (strCompare == 0) {
flag = 0;
printf("Name = %s, Tel. = %s\n", p->name, p->telno);
}
if (flag == 1)
printf("Name not found.\n");
}
}
但我又收到了一些警告。
warning: cast from pointer to integer of different size
warning: initialization makes pointer from integer without a cast
我真的认为如果结构都作为数组传递,我的生活会更轻松,在这种情况下,我可以简单地使用 For 循环遍历结构数组并完成它。这是我在整个高中期间一直在做的事情,但现在我在大学里,我不得不处理作为指针传递的结构。
在这种情况下,我该如何处理 strcmp()
?如何将其应用于任何将结构作为指针传递的程序?
程序模板结构不合理,使用过时且危险的函数给出了一个非常糟糕的示例gets()
。
传递readin()
p
参数指向的数组中的结构数会好得多。从代码模板中,我们必须假设 MAX
结构可通过 p
.
readin
函数应从标准输入读取最多 MAX
个条目,并使用索引或递增 p
将其存储到目标数组的相应元素中。
这是一个带有索引的解决方案:
int readin(PhoneBk *p) {
int i, n = MAX;
for (i = 0; i < n; i++) {
printf("Enter person name, # to stop.\n");
if (scanf("%19s", p[i].name) != 1 || p[i].name[0] == '#')
return i;
printf("Enter the telephone number.\n");
if (scanf("%19s", p[i].telno) != 1)
return i;
}
return i;
}
同样,搜索函数可以遍历从0
到size
的数组排除:
void search(PhoneBk *p, int size, char *target) {
int i, found = 0;
for (i = 0; i < size; i++) {
if (!strcmp(p[i].name, target) {
printf("Name = %s, Tel. = %s\n", p[i].name, p[i].telno);
found++;
}
}
if (found == 0)
printf("Name not found.\n");
}
关于警告,您在 strcmp((char)p->name, "#")
上有一个拼写错误,强制转换是不必要的,在这种情况下将指针转换为单个 char
,导致 2 个警告:一个用于将指针转换为一个不同大小的整数和另一个用于将整数传递给 strcmp()
而不是指向 char
.
在 readin 中,您不测试最后输入的名称,而是测试下一个条目的名称(未初始化),并且您也不检查是否到达 phone 书,也不是 EOF 案例。请注意,您还假设名称只是一个单词并且不能包含空格,但是因为 main 使用(丑陋的) gets 可能是组合名称是允许的。
为了不被多余的空格打扰,似乎需要删除它们。
例如:
#include <ctype.h>
void normalize(char * s)
{
/* remove unexpected 'isspace' */
int p1 = 0, p2 = 0;
while (s[p2] != 0) {
if (!isspace((unsigned char) s[p2]))
s[p1++] = s[p2];
else if ((p1 != 0) && (s[p1 - 1] != ' '))
s[p1++] = ' ';
p2 += 1;
}
s[((p1 != 0) && (s[p1 - 1] == ' ')) ? p1 - 1 : p1] = 0;
}
int readin(PhoneBk *p)
{
int n;
for (n = 0; n != MAX; ++n) {
puts("Enter person name, max length 18, # to stop.");
if (fgets(p->name, sizeof(p->name), stdin) == NULL) {
/* EOF */
return n;
}
normalize(p->name);
if (p->name[0] == '#')
break;
printf("Enter the telephone number, max length 18\n");
if (fgets(p->telno, sizeof(p->telno), stdin) == NULL) {
/* EOF, forget partial entry */
return n;
}
normalize(p->telno);
p += 1;
}
return n;
}
你的 search 太复杂了我宁愿不要试着去理解它,抱歉,定义可以很简单:
void search(PhoneBk *p, int size, char *target)
{
normalize(target);
while (size--) {
if (!strcmp(p->name, target)) {
printf("%s found, phone number is %s\n", target, p->telno);
return;
}
p += 1;
}
printf("unknown name %s\n", target);
}
注意我也对读名进行了规范化,以与本书的读法兼容。
如果我尝试不更改给定的 main 及其丑陋的 gets,编译和执行:
/tmp % gcc -g -pedantic -Wextra c.c
/tmp/ccdXFM1o.o: In function `main':
/tmp/c.c:17: warning: the `gets' function is dangerous and should not be used.
/tmp % ./a.out
Enter person name, max length 18, # to stop.
john do
Enter the telephone number, max length 18
12 34 56
Enter person name, max length 18, # to stop.
just me
Enter the telephone number, max length 18
1 2 3 4 5
Enter person name, max length 18, # to stop.
#
Enter search name:
john do
john do found, phone number is 12 34 56
/tmp %
/tmp % ./a.out
Enter person name, max length 18, # to stop.
john do
Enter the telephone number, max length 18
12 34 56
Enter person name, max length 18, # to stop.
just me
Enter the telephone number, max length 18
1 2 3 4 5
Enter person name, max length 18, # to stop.
#
Enter search name:
just me
just me found, phone number is 1 2 3 4 5
/tmp %
/tmp % ./a.out
Enter person name, max length 18, # to stop.
john do
Enter the telephone number, max length 18
12 34 56
Enter person name, max length 18, # to stop.
just me
Enter the telephone number, max length 18
1 2 3 4 5
Enter person name, max length 18, # to stop.
#
Enter search name:
me
unknown name me