当我尝试将结构指针传递给函数时遇到此错误,它是什么意思,为什么我不能传递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:可以在此处找到整个代码(很抱歉,我仍在学习git)
答案 0 :(得分:2)
将结构声明放在函数原型之前
#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);
答案 1 :(得分: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;
};
OR
在函数原型之前声明结构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);
不保证您的程序没有其他问题。另外我不明白为什么您需要使用指针版本的指针。这似乎容易引起问题。