简体   繁体   中英

Trying to pass a struct pointer to a function results in error

I'm getting this error when I try to pass a struct pointer to a function, what does it mean and why can't I pass a struct* ?

I have multiple functions similar to this one all returning the same error.

I have tried the solution mentioned in Passing struct pointer to function in c . but the same errors are printed. I have attached links below for both programs with and without the soln respectively

**ERROR**:</p>
$ gcc -o class -Wall -Werror -Wwrite-strings -std=gnu99 -ggdb3 classroster2.c
<p >classroster2.c:15:23: 

error: **'struct node' declared inside parameter list will not be visible outside of this definition or declaration [-Werror]**

   15 | void enterName(struct node* xptr);
  

classroster2.c:57:6: **error: conflicting types for 'enterName'**
 
  57 | void enterName(struct node* xptr){
    
classroster2.c:15:6: **note: previous declaration of 'enterName' was here**
  
 15 | void enterName(struct node* xptr);
    struct student{
        size_t noClass;
        char **classTaken;  
    };
    struct node{
        struct student* details; 
        struct node *next;
        char *name;
    
        };

function is here

       void enterName(struct node* xptr){
           printf("\nEnter the name of Student(%d)(max 12)= ",studentNo+1);
               xptr->name=malloc(MAX_LENGTH);
                   alloCheck(xptr->name);
                   scanCheck(scanf("%s", xptr->name));
               studentNames[studentNo] = xptr->name;
               studentNo++;
       }

calling function

            struct node * studenti =NULL;
            init(studenti);    
            //studentname
            enterName(studenti);

PS:The entire code can be found here(sry I'm still learning git)

With ** https://pastebin.com/Yp9hkAL7

put the struct declarations before the function prototypes

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

struct student{
    size_t noClass;
    char **classTaken; 
};
struct node{
    struct student* details;
    struct node *next;
    char *name;
   
};
 
 
void tstpt(void);
void prgmSt(void);
void initHead(void);
 
void scanCheck(int tst);
void alloCheck(void* ptr);
void inputCheck(int x);
 
void init(struct node* wptr);
void enterClassNo(struct node* yptr);
void enterName(struct node* xptr);
void enterClassTaken(struct node* zptr);

You need to either forward declare the structure node before any function prototype or fully declare the structure before any function prototype which uses a pointer to this structure, because the compiler reads the file from top to bottom.

Else the compiler doesn't know what node is when referring to a pointer to this structure as parameter types, as it does not have a reference. It is "confused" and throws a diagnostic which says that you would attempt to fully declare this structure in the parameter lists.


Forward Declaration of structure node :

struct node;

void init(struct node* wptr);
void enterClassNo(struct node* yptr);
void enterName(struct node* xptr);
void enterClassTaken(struct node* zptr);


struct node {
    struct student* details;
    struct node *next;
    char *name;   
};

OR

Declaration of the structure node before the function prototypes:

struct node {
    struct student* details;
    struct node *next;
    char *name;   
};

void init(struct node* wptr);
void enterClassNo(struct node* yptr);
void enterName(struct node* xptr);
void enterClassTaken(struct node* zptr);

No guarantee that your program has no further issues. Also I don't understand why you the need pointer to pointer version. This seems susceptible for an issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM