Reputation: 109
I have a program where I have to add a name, age and 2 courses for a student in a database using pointer structures and pointer arrays in C. I am able to store the name entered by the user into the database but not the age. When I enter age, this error shows up "format specifies type 'int ' but the argument has type 'int' [-Wformat] scanf("%d", ((pArr[i])).age);"
I know that this could be a common error but I am a bit new to C. Any help would be appreciated. My function for entering new values looks like:-
Also, this code is still in development, so please point me out any extra errors if there are :)
//global
#define SIZE 30
#define fieldLength 200
struct db_type
{
char name[fieldLength];
int age;
char course1[fieldLength];
char course2[fieldLength];
char status[fieldLength];
};
struct courseInfo
{
char code [20]; // e.g., EECS2030
char title [fieldLength];
char date [20];
char time_start [20];
char time_end [20];
char location [20];
};
struct courseInfo courseArr[SIZE];
int main(int argc, char *argv[])
{
struct db_type * db_pArr[SIZE]; // main db storage
init_list(db_pArr); // set to NULL
init_courseArr(); // load course from diskfile
char choice;
for(; ;){
choice = prompt_menu();
switch (choice)
{
case 'n': enterNew(db_pArr); break;
case 'q': exit(1); // terminate the whole program
}
}
return 0;
}
void enterNew(struct db_type * pArr[SIZE]){
static int i=0;
static int j=0;
int flag = 0;
pArr[i] = malloc(sizeof(struct db_type));
printf("name: ");
scanf("%s", (*pArr[i]).name);
printf("age: ");
scanf("%d", (*(pArr[i])).age); //error here
printf("course-1: ");
scanf("%s", (*pArr[i]).course1);
while(flag == 0)
for(int j=0; j<SIZE; j++){
if(strcmp((*pArr[i]).course1, courseArr[j].code) == 1 && flag == 0){
printf("course does not exist, enter again: \n");
printf("course-1: ");
scanf("%s", (*pArr[i]).course1);
}
else
flag = 1;
}
if(flag == 1)
++i;
// further code in development
// printf("course-2: ");
// scanf("%s", pArr[i].course2);
}
Further extra information about what program does -> This program basically is a part of a student database management system. When user enters 'n' or 'N', this function is invoked. User has the option to enter student name, age, student's course-1 and course-2. It also has to check whether course-1 and course-2's start time and end time clash or not and store it in a variable called char status[] of structure db_type.
Upvotes: 0
Views: 2423
Reputation: 328
Change
scanf("%d", (*(pArr[i])).age);
to
scanf("%d", &(*(pArr[i])).age);
scanf() requires a pointer to the variable and adding &
in front of it returns the pointer of the variable.
Upvotes: 1