Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c, problems with using struct

Tags:

c

struct

I'm trying to write a program that reads text from external file (string string int, per line). Struct is defined outside of main function:

typedef struct Person {
  char fname[15];
  char lname[20];
  unsigned long int birth;
} clovek;

I don't need "clovek" to be an array as with every line data can be overwritten. Line is red to buffer with:

fgets(buffer, 50, datafile);

Then I want to parse it to the struct but that is where my problem arises:

int i = 0;
while (buffer[i] != ' ') {
  clovek.fname[i] = buffer[i];
  i++;
}

And this gives me out an error: expected identifier or '(' before '.' token

I also wanted to use this code for debugging but it gives out another error as well:

printf("fname, %s\n", clovek.fname);

error: expected expression before 'clovek'

My guess is that I totaly misunderstood using of struct.


1 Answers

clovek is an alias for struct Person. Either remove the typedef keyword, or create an object of type struct Person somewhere in your code. With the present code you can do:

clovek someone;

while (buffer[ i ] ) != ' ') {
    someone.fname[ i ] = buffer[ i ];
/* .. */
like image 174
dirkgently Avatar answered May 30 '26 07:05

dirkgently



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!