[英]Trying to pass a struct pointer to a function results in error
当我尝试将 struct 指针传递给 function 时出现此错误,这是什么意思,为什么我不能传递struct*
?
我有多个与此类似的函数都返回相同的错误。
我已经尝试过在 c 中将结构指针传递给 function 中提到的解决方案。 但会打印相同的错误。 我在下面分别为带有和不带有 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 在这里
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++;
}
调用 function
struct node * studenti =NULL;
init(studenti);
//studentname
enterName(studenti);
PS:完整的代码可以在这里找到(sry我还在学习git)
将结构声明放在 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);
您需要在任何 function 原型之前前向声明结构node
,或者在使用指向该结构的指针的任何 function 原型之前完全声明结构,因为编译器从上到下读取文件。
否则编译器在引用指向该结构的指针作为参数类型时不知道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;
};
或者
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);
不保证您的程序没有其他问题。 另外我不明白为什么你需要指向指针版本的指针。 这似乎很容易出现问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.