C 编程 - 嵌套链接列表打印问题

标签 c

我正在写一份类作业,但遇到了一些问题。与大多数编程类(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/

相关文章:

Python:ctypes 和垃圾收集

我可以假设以较小的大小调用 realloc 将释放剩余部分吗?

c - Linux 中多线程的信号处理

c - 数组 : accessing array elements by inverting the index variable with the array itself yields the same result

编译错误 "s is undeclared,"但它是 `scanf` 的结果

c - 如何编译这个库以供使用?

C程序不会按升序排列数字

c - 如何将文本文件数据分配给变量

java - Java 中的 isprint 等价物

python - 导入错误 : No module name libstdcxx