2

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.

3
  • 1
    All but one of the answers you got provide another frightening example of how people can get very basic C syntax wrong. Commented May 1, 2009 at 19:10
  • let me add another issue of complexity, element alignment in structs. Google is your friend. Commented May 1, 2009 at 19:25
  • not to mention the buffer overflow waiting to happen and the lack of nul termination that makes the members of struct Person useless as C strings. Commented May 1, 2009 at 21:04

2 Answers 2

10

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 ];
/* .. */
Sign up to request clarification or add additional context in comments.

Comments

5

Your statement defines clovek as an alias to the structure Person.

Remove "typedef" from the declaration. This way, clovek becomes a variable of type Person:

Or even better, separate struct declaration from variable declaration:

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

struct Person clovek;

2 Comments

You probably mean "struct Person clovek;" in the second line.
That should be struct Person clovek;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.