-2

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;    
    }
  • strcpy(cararray[0], car1.color); is it valid ? – venki Aug 31 '15 at 07:20
  • No it's not. Sorry I didn't mean to include it. I've spent so many hours on this I'm not even sure where I am anymore. – Davy Duenow Aug 31 '15 at 08:03
  • and more errors are there to resolve. – venki Aug 31 '15 at 08:05
  • I've edited to where the program will function and pull data from the struct member car1 but no other members. My intent is to be able to pull all members. I understand all the criticism of this program however I just started programming less than 3 months ago. This project is our assignment for our class and my TA's have been less than helpful so I was hoping to get more help from here. – Davy Duenow Aug 31 '15 at 08:13

1 Answers1

6

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.
Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • I'm able to get the first member of the structs information. I've edited my post...I added the wrong file. I did at some point had a check for file but not fscanf. But just to be clear if I malloc for carptr I'll be able to view all members of the struct. I'm not sure I was 100% clear on asking that question. I want to be able to access the whole struct and members outside of its current scope. Even if I malloc how would I achieve that? Also thanks for the fast answer. – Davy Duenow Aug 31 '15 at 08:08