首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >C语言接收另一个函数中的结构

C语言接收另一个函数中的结构
EN

Stack Overflow用户
提问于 2018-06-10 07:25:09
回答 1查看 66关注 0票数 -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);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-10 07:51:18

好消息是您在正确的轨道上--坏消息是您的轨道中缺少几个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;
}

示例使用/输出

$ ./bin/struct_find_str
Name: Alfred
t[1].name == Alfred
t[1].name == Alfred
t[i].name == name!
string found at index '1'.

如果您有进一步的问题,请告诉我。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50779356

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档