0

What I am trying to do is use a struct function that takes the values that are contained in the student struct, create a student struct, and then return a student pointer. So basically:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct student_struct
{
    char name[16];
    int age;
    float gpa;

}Student;


Student *makeStudent(char name[16], int age, float gpa)
{
    Student *s = (Student *) malloc(sizeof(Student));
    //FIXME: Make your code changes here....
    strcpy(s->name, name);
    s->age=age;
    s->gpa=gpa;
    return s;

}

//Display the values of all students in the class
void displayClass(Student classArray[], int size)
{
    int i;
    for(i=0;i<size;i++){
        printf("  %s, %d, %.1f\n", classArray[i].name, classArray[i].age, classArray[i].gpa);
    }
}

int main()
{
    //FIXME: Implement make student function
    Student *adam = makeStudent("adam", 18, 2.0);
    Student *beth = makeStudent("beth", 19, 3.2);
    Student *chris = makeStudent("chris", 18, 3.6);
    Student *daphney = makeStudent("daphney", 20, 3.9);
    Student *erin = makeStudent("erin", 19, 2.6);

    const int CLASS_SIZE = 5;
    Student classArray[CLASS_SIZE];
    classArray[0] = *adam;
    classArray[1] = *beth;
    classArray[2] = *chris;
    classArray[3] = *daphney;
    classArray[4] = *erin;

    displayClass(classArray, CLASS_SIZE);

    //deallocate memory
    free(adam);
    free(beth);
    free(chris);
    free(daphney);
    free(erin);

    return 0;
}

And the output I would like to get is:

adam, 18, 2.0
beth, 19, 3.2
chris, 18, 3.6
.... (continues from there)

But instead, what I get is:

 , 0, 0.0
 , 0, 0.0
 , 0, 0.0
 , 0, 0.0
 , 0, 0.0

I think it has to do with the

Student *makeStudent(char name[16], int age, float gpa)

function but I am not so sure. Any hints? Not looking for the answer!

  • Arrays decay to pointers when you pass them to functions –  Nov 28 '18 at 17:38
  • @JETM im sorry what do you mean by that? –  Nov 28 '18 at 17:40
  • The code as posted (with the missing `;` before the `return` fixed) gives the expected results. Make sure that you recompile your code, and that you copy and paste the **exact** code you're compiling. Don't try to retype anything. – dbush Nov 28 '18 at 17:49
  • https://stackoverflow.com/questions/1461432/what-is-array-decaying I'm not sure whether it makes a difference in your specific case, but it's the first suspicious thing I saw. –  Nov 28 '18 at 17:50
  • @dbush I did, got exactly nothing. –  Nov 28 '18 at 17:52
  • @K.Welch What you currently have posted is not the exact code you're running because it does not compile. Post your **real** code via copy/paste. – dbush Nov 28 '18 at 17:53
  • @dbush look again i edited it –  Nov 28 '18 at 17:57
  • Your code as it stands here now works fine. Are you sure the code you run is the code you compile as it stands in the question? – Jabberwocky Nov 28 '18 at 17:57
  • Yeah, it keeps saying the zeros and blanks. Could it be my compiler, I gotta GCC 4.9.2 @Jabberwocky –  Nov 28 '18 at 17:59
  • @K.Welch No, it's not your compiler, gcc works fine. I insist, your code is OK: look here: https://www.ideone.com/dePJNe. How do you compile and run your code? – Jabberwocky Nov 28 '18 at 18:02
  • @Jabberwocky gcc -o name name.c and then ./name –  Nov 28 '18 at 18:04
  • @K.Welch put `printf("Hello Workd\n");` at the very beginning of the `main` function, compile, run and check the output. – Jabberwocky Nov 28 '18 at 18:06
  • @Jabberwocky I got nothing –  Nov 28 '18 at 18:09
  • @K.Welch you mean `Hello World` is not printed? If this is the case then you're somehow not running the program you compile or the program compiles with errors and you're running the previous version – Jabberwocky Nov 28 '18 at 18:10
  • @Jabberwocky Got it working, you just came up with it. I just made a new file! –  Nov 28 '18 at 18:13
  • I'm voting to close this question as off-topic because the code was fine, OP wasn't running the program that he compiled. – Jabberwocky Nov 28 '18 at 18:14

0 Answers0