Thread: Passing structure poiner to a function

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    5

    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
    Last edited by aojsfalsie; 08-24-2014 at 01:17 PM.

  2. #2
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    I think you have to look better at line 14, how you're declaring your function arguments .

  3. #3
    Registered User
    Join Date
    Aug 2014
    Posts
    5
    Thanks Smjert! Wow I feel dumb for missing that, thanks for responding!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing structure to function
    By danieldcc in forum C Programming
    Replies: 4
    Last Post: 10-10-2011, 11:17 AM
  2. Passing Function to Structure
    By C-bob in forum C Programming
    Replies: 11
    Last Post: 07-22-2011, 07:36 PM
  3. poiner to structure problem
    By bluetxxth in forum C Programming
    Replies: 9
    Last Post: 02-25-2010, 04:56 PM
  4. Passing structure to function
    By Sereby in forum C Programming
    Replies: 4
    Last Post: 07-28-2004, 10:05 PM
  5. poiner problem in function
    By caws in forum C Programming
    Replies: 2
    Last Post: 04-22-2003, 06:01 PM