如何在其原始功能之外访问结构的成员? [英] How to access a structures' members outside of its original function?

查看:73
本文介绍了如何在其原始功能之外访问结构的成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目中,在一个不同于main()的单独函数中创建结构.在创建成员(变量)信息并将其添加到结构变量中之后,我需要能够return指向该结构的指针,我可以在其中访问它以进行参考和打印.

I am working on a project where I am creating structures inside of a separate function than main(). After being created and added member (variable) information into the structure variable, I need to be able to return a pointer to the structure in which I can access it for reference and printing.

我包括了我的代码,请尽可能简单,因为我是结构新手.

I am including my code and please be as simple as possible as I am new to structures.

#include <stdio.h>
#include <stdlib.h>
#include<string.h>

typedef struct _car
{
    char color[20];
    char model[20];
    char brand[20];
    int year;
    struct _car *eptr;
} Car;

Car* readCars(char* filename);


/*
 * 
 */
int main(int argc, char** argv) {
    Car *Cars;
    Car *eptr;

    Cars = readCars(argv[1]);

    printf("%s%s%s%s", Cars->color, Cars->brand, Cars->model, Cars->color);

    return (EXIT_SUCCESS);
}

Car* readCars(char* filename)
{
    FILE *file = fopen(filename, "r");
    int year;
    char color [20], model [20], brandx[20];
    Car car1;
    Car car2;
    Car *carptr;
    Car *car2ptr;
    if (file == NULL)
    {
        printf("File cannot be opened\n");
    }
    while(file != EOF)
    {

        fscanf(file, "%s%s%s%d", color, model, brandx, &year);

        strcpy(car1.color, color);
        strcpy(car1.model, model);
        strcpy(car1.brand, brandx);
        car1.year = year;

        carptr = &car1;
        fscanf(file, "%s%s%s%d", color, model, brandx, &year);

        strcpy(car2.color, color);
        strcpy(car2.model, model);
        strcpy(car2.brand, brandx);
        car2.year = year;
        car2ptr = &car2;

        if (fscanf(file, "%s%s%s%d", color, model, brandx, &year) == EOF)
        {
            break;
        }
    }

    return carptr;    
    }

推荐答案

要回答帖子中的主要问题,请在Car* readCars(char* filename)函数中,将局部变量的地址存储到指针中

To answer the main question in your post, in the Car* readCars(char* filename) function, you're storing the address of a local variable into the pointer

carptr = &car1;

,然后尝试return

return carptr;

在这种情况下,一旦readCars()函数完成执行,car的存在就会停止,并且您将调用

In this case, as soon as the readCars() function finisihes execution, the existence of car will be ceased and you'll be invoking undefined behaviour by accessing the returned pointer.

如果您要使某个内存即使在其分配范围之外仍然有效(在这种情况下,在readCars()函数之外),则需要使用动态内存分配来分配该内存,例如,

If you want a memory to be valid even outside the scope of it's allocation (in this case, outside readCars() function), you need to allocate the same using dynamic memory allocation, for example, malloc() and family.

FWIW,使用返回的指针完成操作后,还需要 free() 内存以避免内存泄漏.

FWIW, once you've done using the returned pointer, you also need to free() the memory to avoid memory leak.

那是

  1. 您从未有效地检查fopen()fscanf()是否成功.如果它们中的任何一个失败,则通常继续是调用UB的理想方案.
  2. while ( !feof (fp) )不好
  3. 代码中的指针算术是毫无意义的.
  4. 您有语法错误...也许还有更多.
  1. You never effectively checked for the success of fopen() or that of fscanf(). If either of them failed, continuing normally is the ideal scenario for invoking UB.
  2. while ( !feof (fp) ) is bad
  3. Pointer arithmetic(s) in your code is(are) meaningless.
  4. You've got syntax errors...and maybe more.

这篇关于如何在其原始功能之外访问结构的成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆