c - 尝试将结构指针传递给函数会导致错误
问题描述
当我尝试将 struct 指针传递给函数时出现此错误,这是什么意思,为什么我不能传递 a struct*
?
我有多个与此类似的函数都返回相同的错误。
我已经尝试过将结构指针传递给 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)
解决方案
您需要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);
不保证您的程序没有其他问题。另外我不明白为什么你需要指向指针版本的指针。这似乎很容易出现问题。
推荐阅读
- php - 如何用 PHP 和 MySQL 实现 API 资源扩展
- python - pandas Timedelta 不将月份间隔识别为字符串(`3M` 或 `P3M`)
- curl - 在kafka桥srimzi上启用用户名和密码
- asp-classic - 我应该如何编码使用 XMLHttpRequest 发送的表单值
- javascript - 删除对象数组中的项目:打字稿
- sql - Oracle查询计算与日期数组完全相同的匹配
- c# - 如何使用 MultiSelection="true" 让默认值出现在 MudBlazor MudSelect 中?
- javascript - 如何获取列表项元素的索引而不是“显示:无”;
- python - 将 Matlab 数值运算符转换为 Python 时遇到问题
- python - 无法确认使用 python 从 AWS SNS 订阅 HTTP\S 端点?