I need help with structures. So far my learning experience with C and Xcode has been great, but I am frustrated over structures and typedefs I have copied examples straight out the K&R book, they work, yet when I try my own they don't. The errors for the below code:
19: error: parse error before "readRecords"
19: warning: data definition has no type or storage class
49: warning: initialization makes pointer from integer without a cast
85: error: parse error before "readRecords"
94: error: `gradeRecord' undeclared (first use in this function)
94: error: parse error before "tempRecord"
95: error: `tempRecord' undeclared (first use in this function)
19: error: parse error before "readRecords"
19: warning: data definition has no type or storage class
49: warning: initialization makes pointer from integer without a cast
85: error: parse error before "readRecords"
94: error: `gradeRecord' undeclared (first use in this function)
94: error: parse error before "tempRecord"
95: error: `tempRecord' undeclared (first use in this function)
Code:
//Library inclusions
#include <stdio.h>
#include <ctype.h>
#include <string.h>
//Constants
#define FLUSH while(getchar() != '\n');
#define MAXCHARS 80
//function headers
void prnMenu(int *menuChoice); //prints the menu
p2GradeRecord readRecords(FILE *); //This wrong to the compiler
//I thought when you typedef something it acts just like any other type so
//a function header is simply type identifier(argList);
int main (int argc, const char * argv[]) {
//variables
FILE *inputFile;
typedef struct gradeRecord{
char *firstName;
char *lastName;
int *scores;
}GradeRecord;
typedef struct gradeRecord *p2GradeRecord;
inputFile = fopen("input.txt", "r"); //open the input file
if (inputFile != NULL){
p2GradeRecord gradeList = readRecords(inputFile);
}
do{
prnMenu(menuChoicePtr);
switch ( menuChoice ){
case 1 :
break;
case 2 :
if ( 1){
}
else{
printf("No grades entered!\n");
}
break;
case 3 :
if(1){
}
else{
printf("You must enter scores and compute statistics first!\n");
}
break;
case 4 :
case 5 :
printf("Have a nice day!\n");
return 0;
}
}while(menuChoice != 5);
return 0;
}
p2GradeRecord readRecords(FILE *file){
char fname[MAXCHARS];
char lname[MAXCHARS];
int score1 = 0;
int score2 = 0;
int score3 = 0;
int score4 = 0;
while((fscanf(file, "%s%s%d%d%d%d", fname, lname, &score1, &score2, &score3, &score4)) != EOF){
gradeRecord tempRecord = (gradeRecord) malloc(sizeof(gradeRecord));
if((tempRecord->firstName = (char *) malloc(strlen(fname) * sizeof(char))) == NULL){
printf("\nInsufficient Memory\n");
exit(1);
}
strcpy(tempRecord->firstName, fname);
if((tempRecord->lastName = (char *) malloc(strlen(fname) * sizeof(char))) == NULL){
printf("\nInsufficient Memory\n");
exit(1);
}
strcpy(tempRecord->lastName, lname);
if((tempRecord->scores = (int *) malloc(4 * sizeof(int))) == NULL){
printf("\nInsufficient Memory\n");
exit(1);
}
tempRecord->scores[0] = score1;
tempRecord->scores[1] = score2;
tempRecord->scores[2] = score3;
tempRecord->scores[3] = score4;
}
return &tempRecord;
}