【问题标题】:Trying to pass a struct pointer to a function results in error尝试将结构指针传递给函数会导致错误
【发布时间】:2020-07-08 10:57:07
【问题描述】:

当我尝试将结构指针传递给函数时出现此错误,这是什么意思,为什么我不能传递 struct*

我有多个与此类似的函数都返回相同的错误。

我已经尝试过Passing struct pointer to function in c 中提到的解决方案。但是会打印相同的错误。我在下面分别为带有和不带有 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;
    
        };

功能在这里

       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++;
       }

调用函数

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

PS:完整的代码可以在这里找到(sry我还在学习git)

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

【问题讨论】:

  • 在文件范围内移动结构声明。
  • 阅读 Modern C 然后阅读 C 编译器(可能是 GCC...)和调试器(可能是 GDB...)的文档

标签: c pointers struct


【解决方案1】:

将结构声明放在函数原型之前

#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);

【讨论】:

    【解决方案2】:

    您需要在任何函数原型之前前向声明结构node 或在任何使用指向该结构的指针的函数原型之前完全声明该结构,因为编译器从上到下读取文件。

    否则编译器不知道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;   
    };
    

    在函数原型之前声明结构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);
    

    不保证您的程序没有其他问题。另外我不明白为什么你需要指向指针版本的指针。这似乎很容易出现问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-26
      • 1970-01-01
      • 2020-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-02
      相关资源
      最近更新 更多