我正在写一份类作业,但遇到了一些问题。与大多数编程类(class)一样,他们并不总是使用最佳实践来设置变量和代码,您必须让它发挥作用。
这是设置:
链接.h
//just showing the struct setup
typedef struct listCDT *listADT;
链接.c
//just showing the struct setup as all the other functions work
typedef struct point {
listElementT x;
struct point *next;
} myDataT;
struct listCDT {
myDataT *start; // myDataT *header;
myDataT *end; // myDataT *footer;
};
驱动程序.c
//main is truncated to just the problem area
void main()
{
listADT X, Y;
X = NewList();
Y = NewList();
list_print_values(Y, "Y");
}
void list_print_values(listADT a, char *name)
{
while (a != NULL)
{
printf("%d ", *((*a)->start)->x););
a = (&a)->end;
}
printf("\n");
return;
}
driver.c 是我创建的唯一文件,我当前遇到的唯一问题是打印结构。我收到以下错误:
>make
gcc -c driver.c
driver.c: In function ‘list_print_values’:
driver.c:56:22: error: dereferencing pointer to incomplete type
printf("%d ", *((*a)->start)->x);
^
driver.c:57:11: error: request for member ‘end’ in something not a structure or union
a = (&a)->end;
^
make: *** [driver.o] Error 1
我已经尝试了几乎所有我能想到的东西,但我一定错过了一些简单的东西?有人可以帮忙吗?
最佳答案
可以使用 .
或 ->
运算符访问结构成员。一种与结构指针一起使用,另一种与结构一起使用。
在您的情况下,您正在访问结构指针的成员,因为您使用的是 ->
,所以不需要取消引用。
printf("%d ", (a->start)->x);
a = a->end;
假设listElement
的类型为int
,因此格式说明符为%d
。
关于C 编程 - 嵌套链接列表打印问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49118607/