C语言在另一个函数中接收struct
C language receive struct in another function
当我尝试在 equalname
函数中读取 t[1].name
的值时,它不起作用。我如何将该值发送给另一个函数?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
struct test {
char name[100];
char num[100];
};
int equalname(struct test *t, char string[100]) {
int i;
t = malloc(sizeof(struct test)*100);
printf("t[1].name == %s\n", t[1].name); //prints garbage(t[1].name != name)
for (i = 0; i < 100; i++) //also, is this even right?
{
if (t[i].name == string) //should I use 2 for's and set t[i].name[j] == string[j]?
{
printf("t[i].name == name!");
return i;
break;
}
}
printf("WRONG");
return 0;
}
int main() {
struct test *t;
t = malloc(sizeof(struct test)*100);
char name[100];
printf("Name:\n");
scanf("%s", name);
strcpy(t[1].name, name);
printf("t[1].name == %s\n", t[1].name); //this works (t[1].name == name)
equalname(t[1].name, name);
}
好消息是你走在正确的轨道上——坏消息是你的轨道上缺少一些 rails...
首先,当您使用 malloc
分配内存时,内存块被保留,但块中的所有字节都保持 未初始化 。尝试访问未初始化的内存位置中的任何内容是 未定义的行为 ——你的代码的操作从那时起不再定义——它可能看起来有效或 SegFault 或介于两者之间的任何东西.
您有两个选择,(1) 遍历每个结构并显式初始化值,或者 (2) 使用 calloc
将所有字节分配并初始化为零,例如
#define MAXN 100 /* if you need a constrant #define 1 (or more) */
struct test {
char name[MAXN];
char num[MAXN];
};
...
int main() {
int index;
struct test *t;
char name[MAXN] = "";
t = calloc(MAXN, sizeof *t); /* use calloc, or initialize values */
if (t == NULL) { /* validate every allocation */
perror ("calloc-t");
return 1;
}
不要在equalname
中再次分配t
。虽然这不会覆盖您的原始地址,因为 C 使用按值传递,并且 t
是来自 main 的 t
的副本 - 它也不会为您接受创建另一个未初始化的内存块做任何事情。只需从 main
传递 t
并使用它,例如
int equalname (struct test *t, char *string)
{
int i;
printf("t[1].name == %s\n", t[1].name);
for (i = 0; i < MAXN; i++)
{ /* you must use strcmp to compare strings, not == */
if (strcmp (t[i].name, string) == 0)
{
printf ("t[i].name == name!\n");
return i;
break;
}
}
printf("WRONG");
return -1;
}
接下来,您不能将字符串与 ==
进行比较。您要么必须遍历每个字符并进行比较——要么只使用 strcmp
(这就是它的目的),例如
if (strcmp (t[i].name, string) == 0)
{
printf ("t[i].name == name!\n");
return i;
break; /* break does nothing here */
}
接下来,您必须验证每个分配和每个用户输入——否则您将引发未定义的行为。如果您未能验证分配且未能验证输入——您可能无法相信您实际上是在代码中的有效内存中处理有效数据。
总而言之,您可以执行以下操作:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define MAXN 100 /* if you need a constrant #define 1 (or more) */
struct test {
char name[MAXN];
char num[MAXN];
};
int equalname (struct test *t, char *string)
{
int i;
printf("t[1].name == %s\n", t[1].name);
for (i = 0; i < MAXN; i++)
{ /* you must use strcmp to compare strings, not == */
if (strcmp (t[i].name, string) == 0)
{
printf ("t[i].name == name!\n");
return i;
break;
}
}
printf("WRONG");
return -1; /* on error return a value that cannot be a valid index */
}
int main() {
int index;
struct test *t;
char name[MAXN] = "";
t = calloc(MAXN, sizeof *t); /* use calloc, or initialize values */
if (t == NULL) { /* validate every allocation */
perror ("calloc-t");
return 1;
}
printf ("Name: ");
if (scanf ("%s", name) != 1) { /* validate ALL user input */
fprintf (stderr, "error: invalid input - name.\n");
return 1;
}
strcpy (t[1].name, name);
printf ("t[1].name == %s\n", t[1].name);
index = equalname (t, name); /* save the return! */
if (index == -1) { /* validate the operation of your function */
fprintf (stderr, "string '%s' not found.\n", name);
return 1;
}
printf ("string found at index '%d'.\n", index);
return 0;
}
示例Use/Output
$ ./bin/struct_find_str
Name: Alfred
t[1].name == Alfred
t[1].name == Alfred
t[i].name == name!
string found at index '1'.
如果您还有其他问题,请告诉我。
当我尝试在 equalname
函数中读取 t[1].name
的值时,它不起作用。我如何将该值发送给另一个函数?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
struct test {
char name[100];
char num[100];
};
int equalname(struct test *t, char string[100]) {
int i;
t = malloc(sizeof(struct test)*100);
printf("t[1].name == %s\n", t[1].name); //prints garbage(t[1].name != name)
for (i = 0; i < 100; i++) //also, is this even right?
{
if (t[i].name == string) //should I use 2 for's and set t[i].name[j] == string[j]?
{
printf("t[i].name == name!");
return i;
break;
}
}
printf("WRONG");
return 0;
}
int main() {
struct test *t;
t = malloc(sizeof(struct test)*100);
char name[100];
printf("Name:\n");
scanf("%s", name);
strcpy(t[1].name, name);
printf("t[1].name == %s\n", t[1].name); //this works (t[1].name == name)
equalname(t[1].name, name);
}
好消息是你走在正确的轨道上——坏消息是你的轨道上缺少一些 rails...
首先,当您使用 malloc
分配内存时,内存块被保留,但块中的所有字节都保持 未初始化 。尝试访问未初始化的内存位置中的任何内容是 未定义的行为 ——你的代码的操作从那时起不再定义——它可能看起来有效或 SegFault 或介于两者之间的任何东西.
您有两个选择,(1) 遍历每个结构并显式初始化值,或者 (2) 使用 calloc
将所有字节分配并初始化为零,例如
#define MAXN 100 /* if you need a constrant #define 1 (or more) */
struct test {
char name[MAXN];
char num[MAXN];
};
...
int main() {
int index;
struct test *t;
char name[MAXN] = "";
t = calloc(MAXN, sizeof *t); /* use calloc, or initialize values */
if (t == NULL) { /* validate every allocation */
perror ("calloc-t");
return 1;
}
不要在equalname
中再次分配t
。虽然这不会覆盖您的原始地址,因为 C 使用按值传递,并且 t
是来自 main 的 t
的副本 - 它也不会为您接受创建另一个未初始化的内存块做任何事情。只需从 main
传递 t
并使用它,例如
int equalname (struct test *t, char *string)
{
int i;
printf("t[1].name == %s\n", t[1].name);
for (i = 0; i < MAXN; i++)
{ /* you must use strcmp to compare strings, not == */
if (strcmp (t[i].name, string) == 0)
{
printf ("t[i].name == name!\n");
return i;
break;
}
}
printf("WRONG");
return -1;
}
接下来,您不能将字符串与 ==
进行比较。您要么必须遍历每个字符并进行比较——要么只使用 strcmp
(这就是它的目的),例如
if (strcmp (t[i].name, string) == 0)
{
printf ("t[i].name == name!\n");
return i;
break; /* break does nothing here */
}
接下来,您必须验证每个分配和每个用户输入——否则您将引发未定义的行为。如果您未能验证分配且未能验证输入——您可能无法相信您实际上是在代码中的有效内存中处理有效数据。
总而言之,您可以执行以下操作:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define MAXN 100 /* if you need a constrant #define 1 (or more) */
struct test {
char name[MAXN];
char num[MAXN];
};
int equalname (struct test *t, char *string)
{
int i;
printf("t[1].name == %s\n", t[1].name);
for (i = 0; i < MAXN; i++)
{ /* you must use strcmp to compare strings, not == */
if (strcmp (t[i].name, string) == 0)
{
printf ("t[i].name == name!\n");
return i;
break;
}
}
printf("WRONG");
return -1; /* on error return a value that cannot be a valid index */
}
int main() {
int index;
struct test *t;
char name[MAXN] = "";
t = calloc(MAXN, sizeof *t); /* use calloc, or initialize values */
if (t == NULL) { /* validate every allocation */
perror ("calloc-t");
return 1;
}
printf ("Name: ");
if (scanf ("%s", name) != 1) { /* validate ALL user input */
fprintf (stderr, "error: invalid input - name.\n");
return 1;
}
strcpy (t[1].name, name);
printf ("t[1].name == %s\n", t[1].name);
index = equalname (t, name); /* save the return! */
if (index == -1) { /* validate the operation of your function */
fprintf (stderr, "string '%s' not found.\n", name);
return 1;
}
printf ("string found at index '%d'.\n", index);
return 0;
}
示例Use/Output
$ ./bin/struct_find_str
Name: Alfred
t[1].name == Alfred
t[1].name == Alfred
t[i].name == name!
string found at index '1'.
如果您还有其他问题,请告诉我。