I just started learning C and am not sure what the issue is here.
EDIT
#include <stdio.h>
int main (int argc, const char * argv[]) {
struct student {
int age;
char gender;
char course[30];
};
defineNewStudent("Jarryd", 24, 'M', "Software Engineering");
return 0;
}
void defineNewStudent(char studentName[20], int age, char gender, char course[30])
{
student studentName[30];
studentName.age = age;
studentName.gender = gender;
studentName.course = course[20];
printf("%s is %d.\n Gender: %c.\n Course: %s.\n", studentName, studentName.age, studentName.gender, studentName.course);
}
I have a warning
warning: implicit declaration of function ‘defineNewStudent’
I am trying to take the passed in argument and use it to name the struct, how is this done?
What is this warning about and what are the consequences?
Thanks
The error seems to be because the struct
studentis only defined inside the functionmain. You’re trying to use it in thedefineNewStudentfunction, but that struct is not defined there. Define the struct at the global scope.The warning is because you’re trying to call the
defineNewStudentfunction before you actually declare it, and the compiler still doesn’t know about it at that point. You can declare the function before you try to use it.EDIT:
to define an instance of a struct in C you need the
structkeyword, so in the example it should beIf it were c++, you could drop the
structkeyword:optionally in C, you can typedef the struct to an alias to make it easier to define instances of it: