Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

c - How to access a structures' members outside of its original function?

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
");
    }
    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;    
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

and then trying to return it

return carptr;

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.

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, once you've done using the returned pointer, you also need to free() the memory to avoid memory leak.


That said,

  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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...