hi guys,Iam learning the c language and still a green one really.I have this program am trying to develop using code blocks 10.5.Th code is as follows;


  struct secguard
    char first_name[40];
    char Last_name[40];
    char gender;
    int age;
    int ID;
    char start_date;
    int work_rec;

void main(void)
    struct secguard guard_rec[300];
    char operation;
    char gender;
    if((GUARD_DETAILS=fopen("security.dat","wb+"))== NULL)
        printf("File can't open!");

        printf("Please choose the operation you want to perform.\n");

        case 'A' :printf("Add record(s) to the database.\n");
        case 'U' :printf("Update record(s).\n");
        case 'Q' :printf("Query database to obtain information.\n");
        default :printf("Undefined operation!");


        printf("Please,enter surname.\n");
        printf("Please,enter lastname.\n");
        printf("Please,enter gender.\n");
        printf("Sorry you have no more choices!");


when I compiled this code,it generated errors both being of the same nature.it read that first_name is a member of something that isn't a structure or union.Now,where could I have gone wrong?This is just a small extract.

guard_rec is an array, not a struct instance. You need to index into the array before any member access can be done:

/* Where i is a suitable index */

Your created an array of structures

struct secguard guard_rec[300];

So to access one element of your array you must use an array index like


Oh by the way the use of gets is really frowned upon...really. Here's why

Never use gets(). Because it is impossible to tell without knowing the
data in advance how many characters gets() will read, and because
gets() will continue to store characters past the end of the buffer, it
is extremely dangerous to use. It has been used to break computer
security. Use fgets() instead.

Ooops Narue got here first

Hi guys,thanks for the remedies.However on using fgets the compiler generated two errors of the same nature.It was like 'too few arguements passed to function 'fgets''.I am trying to read more about fgets and I will see what i'll come up with.For now thanks!

Hi guys,thanks for the remedies.However on using fgets the compiler generated two errors of the same nature.It was like 'too few arguements passed to function 'fgets''.I am trying to read more about fgets and I will see what i'll come up with.For now thanks!

Take this in the way its intended...or not. Did you actually google how to use fgets()?

hi guys,i thank you very much for the time you spend for guys like me who are new to the language.while searching for the use of fgets,i came across this individual who also happened to use this platform.he wanted to be advised on how to use fgets() and he was advised to use getchar instead and his response was positive.Here is the code:

1. // Data Structure
2. typedef struct{
3. char first_name[20];
4. char middle_name[20];
5. char last_name[20];
6. char illness_type[5];
7. int patient_number;
8. char doctor_fname[20]; // Doctor's First Name
9. char doctor_lname[20]; // Doctor's Last Name
10. char emer_fname[20]; // Emergency Contact's First Name
11. char emer_lname[20]; // Emergency Contact's Last Name
12. int emer_telephone; // Emergency Contact's Number 13.} PATIENT_DATA;
15.// Defining data type
16.PATIENT_DATA section_1[40]= {"","","","",0,"","","","",0}; 17.  18.//METHOD 1
19.printf("\nPatient First Name: ");
21.fgets(section_1[tally1].first_name, sizeof section_1[tally1].first_name, stdin);
23.//METHOD 2
24.printf("\nPatient First Name: ");
26.fgets(section_1[tally1].first_name, sizeof section_1[20].first_name, stdin);

I just want to learn something from this code.Why did this fella bring in line 16?Can you guys comment on my code because i tried to compile it and it crashed.Anybody that can educate me about fgets is welcome for my code i used gets which didn't generate any errors like fgets.
mixmags,thanks for the link.

This code has some things that you should avoid like


This is a no no.

Plus this

PATIENT_DATA section_1[40]= {"","","","",0,"","","","",0};

I'm not sure what the effect is here...Does it initialize the first element? I'm not really sure, one of the C language lawyers will have to comment on this one..

Here's a cleaned up version of the posted code

#include <stdio.h>

typedef struct
	char first_name[20];
	char middle_name[20];
	char last_name[20];
	char illness_type[5];
	int patient_number;
	char doctor_fname[20];
	char doctor_lname[20];
	char emer_fname[20];
	char emer_lname[20];
	int emer_telephone;

int main()

	PATIENT_DATA section_1 = {"","","","",0,"","","","",0};
	printf("\nPatient First Name: ");

	fgets(section_1.first_name, sizeof(section_1.first_name), stdin);

	return 0;

Please note, if you post code use the code tags.