Basic C help: Function declarations
I just started learning C and am not sure what the issue is here.
开发者_如何学JAVAEDIT
#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
C doesn't have methods. You're trying to define a function in a function. You can't declare a function inside main().
You also have studentName declared twice inside defineNewStudent(), once as the first parameter, and you declare something with the same name inside it.
Also assigning studentName.course = course won't work. You would have to do something like
strncpy(studentName.course, course, sizeof(studentName.course));
You have to define the function before it's use. You can either move the entire thing before main which is what I would do, or you could include just it's prototype before main which here would be:
void defineNewStudent(char studentName[20], int age, char gender, char course[30]);
You have a few problems - nesting functions is probably a bad idea, for one. Take defineNewStudent()
and put it outside of main()
. Depending on where you put it (before or after main()
), you may also need a function prototype before the call site in main()
.
Also, you need to change the array parameter declarations for defineNewStudent()
to look like char parameterName[20]
, instead of what you have there.
You're also going to have some problems with studentName
being the name of two different variables, with trying to copy a string using simple assignment, and with "Software Engineering"
being too big to fit in your arrays.
And as @arsenm mentions in his answer, C doesn't have methods.
The error seems to be because the struct student
is only defined inside the function main
. You're trying to use it in the defineNewStudent
function, but that struct is not defined there. Define the struct at the global scope.
The warning is because you're trying to call the defineNewStudent
function 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.
// define the student struct at global scope
struct student
{
int age;
char gender;
char course[20];
};
// declare the function (it can still be defined later)
void defineNewStudent(char studentName[20], int age, char gender, char course[20]);
int main (int argc, const char * argv[])
{
defineNewStudent("Jarryd", 24, 'M', "Software Engineering");
return 0;
}
void defineNewStudent(char studentName[20], int age, char gender, char course[20])
{
// you already have a variable called studentName,
// you can't have two variables with the same name
struct student studentData;
studentData.age = age;
studentData.gender = gender;
studentData.course = course;
printf("%s is %d.\n Gender: %c.\n Course: %s.\n",
studentName,
studentData.age,
studentData.gender,
studentData.course
);
}
EDIT:
to define an instance of a struct in C you need the struct
keyword, so in the example it should be
struct student studentData;
studentData.age = age;
studentData.gender = gender;
studentData.course = course;
If it were c++, you could drop the struct
keyword:
student studentData;
studentData.age = age;
studentData.gender = gender;
studentData.course = course;
optionally in C, you can typedef the struct to an alias to make it easier to define instances of it:
typedef struct student
{
int age;
char gender;
char course[20];
} StudentStruct;
// define it like this in the function:
StudentStruct studentData;
I agree with Carl. You actually have to define the function before you use it and for that either you put your function above main() or put a function prototype before main:
void defineNewStudent(char studentName[20], int age, char gender, char course[20]);
main() ...rest of code
And you will have one more problem due to the fact the parameter and a local variable of your function have same name which will give a compiler error. Also there are functions in C and not methods(which are in JAVA/C++ etc)
精彩评论