0

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;
}
Nisarg
  • 1,631
  • 6
  • 19
  • 31
Lance
  • 13
  • 4
  • 4
    You never allocated any memory for `my_record` to point to. Why are you using a pointer instead of just a regular struct? – Barmar Jun 13 '18 at 17:39
  • 1
    `abc my_record = malloc(sizeof *my_record);` – Barmar Jun 13 '18 at 17:40
  • 3
    Also see [is it a good idea to typedef pointers](https://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers) – Barmar Jun 13 '18 at 17:40
  • 1
    Although it might not be obvious, in this code you loaded the gun, aimed precisely at your foot, and fired. `abc` is a random pointer, it's never initialized properly, and when you use it you're experiencing *undefined behaviour*. – tadman Jun 13 '18 at 17:47
  • 1
    It's important to understand that you have defined `abc` to be a type that is a pointer to `struct rec`s. That's why @tadman is saying that `my_record` is a random pointer. – bruceg Jun 13 '18 at 18:42
  • It is a poor programming practice to hide pointers inside a typedef – user3629249 Jun 15 '18 at 14:57
  • The callable function: `system()` is exposed in the header file: `stdlib.` However, the posted code is missing the `#include` statement for that header file. – user3629249 Jun 15 '18 at 14:59
  • Error messages should be output to `stderr`, not `stdout` and when the 'error' is from a C library function, should also output the text reason the system thinks the error occurred. One easy way to handle that operation is to call `perror( "your error message" );` – user3629249 Jun 15 '18 at 15:03

3 Answers3

3

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;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
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;
}
Achal
  • 11,821
  • 2
  • 15
  • 37
1

Try this:

// Include this
#include <stdlib.h>

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

int main() {
    ...

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

    ...
}
Alyson Maia
  • 802
  • 5
  • 14