Assign value to a pointer of structure

Lance Source

I'm trying to assign value to my_record but the compiler keeps indicating that my line my_record->x = counter; is having an error:

uninitialized local variable 'my_record' used.

#include<stdio.h>

typedef struct rec
{
    int x, y, z;
} *abc;

int main()
{
    int counter;
    FILE *ptr_myfile;
    //struct rec my_record;
    abc my_record;

    ptr_myfile = fopen("test.bin", "wb");
    if (!ptr_myfile)
    {
        printf("Unable to open file!");
        return 1;
    }
    for (counter = 1; counter <= 10; counter++)
    {
        my_record->x = counter;
        fwrite(&my_record, sizeof(abc), 1, ptr_myfile);
    }
    fclose(ptr_myfile);
    system("pause");
    system("pause");
    return 0;
}
c

Answers

answered 3 months ago Barmar #1

You have several problems.

First, you didn't allocate memory for my_record to point to. The warning about using an uninitialized variable is because you didn't do:

abc my_record = malloc(sizeof(struct rec));

Second, the first argument to fwrite() should be the pointer to the structure you want to write, but you're using a pointer to the pointer.

Third, the second argument to fwrite() should be the size of the structure, but you're giving the size of the pointer.

There doesn't seem to be any good reason to define abc as a pointer in the first place. You should just declare a variable containing the structure itself.

#include<stdio.h>

typedef struct rec
{
    int x, y, z;
} abc;

int main()
{
    int counter;
    FILE *ptr_myfile;
    //struct rec my_record;
    abc my_record;

    ptr_myfile = fopen("test.bin", "wb");
    if (!ptr_myfile)
    {
        printf("Unable to open file!");
        return 1;
    }
    for (counter = 1; counter <= 10; counter++)
    {
        my_record.x = counter;
        fwrite(&my_record, sizeof my_record, 1, ptr_myfile);
    }
    fclose(ptr_myfile);
    system("pause");
    return 0;
}

answered 3 months ago Alyson Maia #2

Try this:

// Include this
#include <stdlib.h>

typedef struct rec {
    int x, y, z;
} abc;

int main() {
    ...

    abc *my_record = (abc*) malloc( sizeof(abc) );

    ...
}

answered 3 months ago achal #3

There are couple of issues in your code like

  • In this abc my_record; the abc is struct rec* and compiler is rightly complaining about my_record not being initialized. As it looks like

    struct rec *my_record; /* un-initilized struct ptr */

    So first allocate the memory for it like

    abc my_record = malloc(sizeof(struct rec));

  • Here in fwrite(&my_record, sizeof(abc), 1, ptr_myfile); the second argument sizeof(abc) is wrong as abc is structure pointer, it won't yield in correct result. Also first arguement of fwrite() is wrong, as it should be pointer to structure my_record not &my_record.

    fwrite(my_record, sizeof(struct rec), 1, ptr_myfile);

And also you want to see Is it a good idea to typedef pointers? After reading this link you might want to avoid typedef a pointer as you did above in your code & you want to do like below

typedef struct rec {
    int x, y, z;
}abc;
int main(void) {
    int counter;
    FILE *ptr_myfile;
    abc *my_record = malloc(sizeof(struct rec));
    ptr_myfile = fopen("test.bin", "wb");
    if (!ptr_myfile) {
        printf("Unable to open file!");
        return 1;
    }
    for (counter = 1; counter <= 10; counter++) {
        my_record->x = counter;
        fwrite(my_record, sizeof(abc), 1, ptr_myfile);
    }
    fclose(ptr_myfile);
    return 0;
}

comments powered by Disqus