有人可以帮助我使用 C 中的结构吗?
Can someone help me with structs in C?
为什么只打印未找到。这个想法当然是用户输入一个名字,然后打印他们的名字和号码。
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
typedef struct
{
char* name;
char* number;
}
person;
int main(void)
{
person people[3];
people[0].name = "sam";
people[0].number = "99912222";
people[1].name = "tom";
people[1].number = "11122222";
people[2].name = "harry";
people[2].number = "12299933";
char boot;
printf("Enter: ");
scanf("%c", &boot);
for(int i = 0; i < 3; i++)
{
if(strcmp(people[i].name, &boot) == 0)
{
printf("%s=%s\n",people[i].name, people[i].number);
return 0;
}
}
printf("Not Found\n");
return 1;
}
%c
表示从输入中获取单个字符。因此,如果你输入'abc',你将得到的只是'a'.
另一个非常重要的事情是,boot
被声明为单个字符,即 char boot;
.
一个char
占用内存1个字节。因此,当您将 &boot
(指向 boot
的地址的指针)传递给 strcmp()
时,它会首先检查参数的长度。它将如何找到它的长度?很简单,通过查找内存中的 '\0' 字符。由于您已将 boot
声明为单个字符,因此它仅包含输入的第一个字符。因此,strcmp()
可能会访问可能不应该访问的内存。这导致未定义的行为。为避免这种情况,您需要保持 boot
至少 n 个字节,其中 n > 1 并且第 n 个字节必须是空字节,如果在第一个和第二个之间没有其他空字节的话第 n 个字节。
简短说明:据我所知,指针指向您分配给它的第一个字节的地址。
所以,
...
// imagine buf[0] is 0x121 and buf[1] is 0x122
char buf[2];
char *ptr = buf;
// ptr will point to 0x121
...
尝试:
...
int nbytes_to_accept = 128;
char boot[nbytes_to_accept + 2];
/* name (nbytes_to_accept) +
* [Extra character to check if name is higher than the max] +
* LF/NULL
* = nbytes_to_accept + 2
*/
printf("Enter: ");
fgets(boot, sizeof boot, stdin);
boot[strcspn(boot, "\n")] = '[=11=]'; // remove LF, as we don't need it
if (strlen(boot) > nbytes_to_accept) {
printf("ERROR: Name cannot be larger than %d characters.\n", nbytes_to_accept);
return 1;
}
/* Make sure to properly get the size of
* the elements of the list, without
* doing a sizeof on a pointer, if this
* list becomes extendable and stuff.
* One way would be to store the size somewhere
*/
for(int i = 0; i < 3; i++)
{
if(strcmp(people[i].name, boot) == 0)
{
printf("%s=%s\n",people[i].name, people[i].number);
return 0;
}
}
printf("Not Found\n");
return 1;
}
为什么只打印未找到。这个想法当然是用户输入一个名字,然后打印他们的名字和号码。
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
typedef struct
{
char* name;
char* number;
}
person;
int main(void)
{
person people[3];
people[0].name = "sam";
people[0].number = "99912222";
people[1].name = "tom";
people[1].number = "11122222";
people[2].name = "harry";
people[2].number = "12299933";
char boot;
printf("Enter: ");
scanf("%c", &boot);
for(int i = 0; i < 3; i++)
{
if(strcmp(people[i].name, &boot) == 0)
{
printf("%s=%s\n",people[i].name, people[i].number);
return 0;
}
}
printf("Not Found\n");
return 1;
}
%c
表示从输入中获取单个字符。因此,如果你输入'abc',你将得到的只是'a'.
另一个非常重要的事情是,boot
被声明为单个字符,即 char boot;
.
一个char
占用内存1个字节。因此,当您将 &boot
(指向 boot
的地址的指针)传递给 strcmp()
时,它会首先检查参数的长度。它将如何找到它的长度?很简单,通过查找内存中的 '\0' 字符。由于您已将 boot
声明为单个字符,因此它仅包含输入的第一个字符。因此,strcmp()
可能会访问可能不应该访问的内存。这导致未定义的行为。为避免这种情况,您需要保持 boot
至少 n 个字节,其中 n > 1 并且第 n 个字节必须是空字节,如果在第一个和第二个之间没有其他空字节的话第 n 个字节。
简短说明:据我所知,指针指向您分配给它的第一个字节的地址。 所以,
...
// imagine buf[0] is 0x121 and buf[1] is 0x122
char buf[2];
char *ptr = buf;
// ptr will point to 0x121
...
尝试:
...
int nbytes_to_accept = 128;
char boot[nbytes_to_accept + 2];
/* name (nbytes_to_accept) +
* [Extra character to check if name is higher than the max] +
* LF/NULL
* = nbytes_to_accept + 2
*/
printf("Enter: ");
fgets(boot, sizeof boot, stdin);
boot[strcspn(boot, "\n")] = '[=11=]'; // remove LF, as we don't need it
if (strlen(boot) > nbytes_to_accept) {
printf("ERROR: Name cannot be larger than %d characters.\n", nbytes_to_accept);
return 1;
}
/* Make sure to properly get the size of
* the elements of the list, without
* doing a sizeof on a pointer, if this
* list becomes extendable and stuff.
* One way would be to store the size somewhere
*/
for(int i = 0; i < 3; i++)
{
if(strcmp(people[i].name, boot) == 0)
{
printf("%s=%s\n",people[i].name, people[i].number);
return 0;
}
}
printf("Not Found\n");
return 1;
}