简体   繁体   English

尝试将结构指针传递给 function 会导致错误

[英]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* ?当我尝试将 struct 指针传递给 function 时出现此错误,这是什么意思,为什么我不能传递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 .我已经尝试过在 c 中将结构指针传递给 function 中提到的解决方案。 but the same errors are printed.但会打印相同的错误。 I have attached links below for both programs with and without the soln respectively我在下面分别为带有和不带有 soln 的两个程序附加了链接

**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 function 在这里

       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调用 function

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

PS:The entire code can be found here(sry I'm still learning git) PS:完整的代码可以在这里找到(sry我还在学习git)

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

put the struct declarations before the function prototypes将结构声明放在 function 原型之前

#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.您需要在任何 function 原型之前前向声明结构node ,或者在使用指向该结构的指针的任何 function 原型之前完全声明结构,因为编译器从上到下读取文件。

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.否则编译器在引用指向该结构的指针作为参数类型时不知道node是什么,因为它没有引用。 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 :结构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: function原型之前的结构node声明:

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.这似乎很容易出现问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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