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

问题描述 投票:-1回答:1

我正在为课堂写作,我遇到了一些问题。与大多数编程类一样,它们并不总是使用最佳实践来设置变量和代码,您必须使其工作。

这是设置:

link.h

//just showing the struct setup
typedef struct listCDT *listADT;

link.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;
};

driver.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

我已经尝试了几乎所有我能想到的东西,而且我一定会错过一些简单的东西?有人可以帮忙吗?

c
1个回答
1
投票

可以使用.->运算符访问结构成员。一个用于结构指针,另一个用于结构。在您的情况下,您正在访问结构指针的成员,因为您使用->,您不需要取消引用。

printf("%d   ", (a->start)->x);
    a = a->end;

假设listElementtype int,因此格式说明符%d

© www.soinside.com 2019 - 2024. All rights reserved.