the console is showing the following when I am trying to run it. can someone tell me how to fix it: arrow.c:3:23: warning: 'struct student' declared inside parameter list will not be visible outside of this definition or declaration
void printInfo(struct student s1);
^~~~~~~
arrow.c: In function 'main':
arrow.c:13:15: error: type of formal parameter 1 is incomplete
printInfo(s1);
^~
arrow.c: At top level:
arrow.c:18:6: error: conflicting types for 'printInfo'
void printInfo(struct student s1){
^~~~~~~~~
arrow.c:3:6: note: previous declaration of 'printInfo' was here
void printInfo(struct student s1);
void printInfo(struct student s1);
struct student {
int roll;
float cgpa;
char name[100];
};
int main(){
struct student s1 = {1664, 9.2, "shradha"};
printInfo(s1);
return 0;
}
void printInfo(struct student s1){
printf("student information ; \n");
printf("student.roll = %d\n", s1.roll);
printf("student.name = %s\n", s1.name);
printf("student.cgpa = %f \n ", s1.cgpa);
s1.roll = 4558;
}
CodePudding user response:
formal parameter 1 is incomplete
As soon as you reference anything where its size is required to be known by the compiler, you must have shown the compiler its definition. In your case it's just a matter of reorganizing the declarations and definitions:
#include <stdio.h>
struct student {
int roll;
float cgpa;
char name[100];
};
// struct student is now defined and ok to take by value in this
// forward declaration:
void printInfo(struct student s1);
int main(void) {
struct student s1 = {1664, 9.2, "shradha"};
printInfo(s1);
}
void printInfo(struct student s1) {
printf("student information ; \n");
printf("student.roll = %d\n", s1.roll);
printf("student.name = %s\n", s1.name);
printf("student.cgpa = %f \n ", s1.cgpa);
s1.roll = 4558;
}
Note: if printInfo
had taken a struct student*
instead, the size of a struct student
would not need to be known since the size of a pointer is already known and is the same for all objects. This would also make the assignment of roll
useful.
Example:
#include <stdio.h>
struct student;
void printInfo(struct student *s1);
struct student {
int roll;
float cgpa;
char name[100];
};
int main(void) {
struct student s1 = {1664, 9.2, "shradha"};
printInfo(&s1);
printf("now visible at the call site: %d\n", s1.roll); // prints 4558
}
void printInfo(struct student *s1){
printf("student information ; \n");
printf("student.roll = %d\n", s1->roll);
printf("student.name = %s\n", s1->name);
printf("student.cgpa = %f\n", s1->cgpa);
s1->roll = 4558;
}