Passing structure poiner to a function
Hi all,
I am trying to wright a program that takes student grade data from a command line file, calculates a final grade, and copies the final grades to an output file. So far I have two functions, one that creates a student structure by allocating memory for it and returning its address, and one that should take that address and fill it with data from a line from the input file. My ultimate goal is to use a linked list to connect all the structs, but for now I just want to get the functions working. When I run what I have so far, I get an error C2440 (using visual 2010) that says "cannot convert from 'cStudent *', to 'cStudent', and points to the line where I call my fill function. Can somebody explain why this is happening? How should structure pointers be passed? Thanks in advance.
here is my code so far...
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student // Declaring student structure globally.
{
char name[50];
int q1, q2, q3, q4, mid1, mid2, final;
struct student *next;
};
typedef struct student cStudent;
cStudent* createStudent(); // allocates memory for a cStudent struct and returns the address.
int fill_student_from_file(FILE *, cStudent); // take the allocated memory and fill it with data from file line.
int main(int argc, char *argv[])
{
FILE *input;
cStudent *astudent;
input = fopen(argv[1], "r"); // open the input file
astudent = createStudent();
fill_student_from_file(input, astudent); // this is where the error occurs
printf("%s:\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n");
fclose(input);
return 0;
}
cStudent* createStudent()
{
cStudent *pStudent;
pStudent = (cStudent*)malloc(sizeof(cStudent));
return pStudent;
}
int fill_student_from_file(FILE *in, cStudent *stu)
{
char *temp = (char*) malloc(sizeof(char)*256);
char *tok;
if(fgets(temp, 256, in) == NULL)
return 0;
else
{
tok = strtok(temp, ",");
strcpy(stu->name, tok);
tok = strtok(NULL, ",");
stu->q1 = atoi(tok);
tok = strtok(NULL, ",");
stu->q2 = atoi(tok);
tok = strtok(NULL, ",");
stu->q3 = atoi(tok);
tok = strtok(NULL, ",");
stu->q4 = atoi(tok);
tok = strtok(NULL, ",");
stu->mid1 = atoi(tok);
tok = strtok(NULL, ",");
stu->mid2 = atoi(tok);
tok = strtok(NULL, ",");
stu->final = atoi(tok);
stu->next = NULL;
free(temp);
return 1;
}
}
also, here is a sample of what a line from the input file would look like:
Bill Gates, 60, 54, 38, 62, 65, 60, 50