1

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;
}
dbush
  • 205,898
  • 23
  • 218
  • 273
  • 1
    _"is showing error"_ - what error? A compilation error? A runtime error? Please copy and paste the full error into the question. – Ted Lyngmo Oct 14 '22 at 17:27
  • `s1.roll = 4558;` is pointless. – Fiddling Bits Oct 14 '22 at 17:28
  • You need to declare `struct student` before using it as a parameter. A forward declaration is sufficient. The problem is that the first occurrence of `struct student` is inside a parameter list, which makes it scoped to the parameter list and unusable outside it. That's what the warning is telling you: "You are declaring a type inside a scope, and it won't be accessible outside the scope, so nobody will be able to call your function." [Discussed here](https://stackoverflow.com/questions/66929337/warning-struct-task-struct-declared-inside-parameter-list-will-not-be-visibl). – Raymond Chen Oct 14 '22 at 17:35

1 Answers1

2

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;
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108