【发布时间】:2016-06-16 12:27:56
【问题描述】:
我很难理解我的代码有什么问题,
正如你所看到的,它是一个链表,它应该连接“Boxes”,每个 Box 都有一个 int 值和一个指向下一个 Box 的指针,但是我无法让它运行而且我没有'不知道为什么。
谁能告诉我为什么它没有运行?
#include <stdio.h>
#include <stdlib.h>
struct node {
int n;
struct node *next;
} node;
typedef struct node Box;
main()
{
Box *q, *r, *s;
Box t;
q = (Box *)malloc(sizeof(Box));
r = (Box *)malloc(sizeof(Box));
s = (Box *)malloc(sizeof(Box));
q->n = 2;
q->next->n = 3;
r->n = 4;
q->next->next = r;
r->next = NULL;
s->n = 5;
s->next = NULL;
t.n = 6;
printf("================");
printf("Q == %d\n", q->n);
printf("R == %d\n", r->n);
printf("S == %d\n", s->n);
printf("{%d}{%d}{%d}{%d}{%d}", q->n, q->next->n, r->n, s->n, t.n);
}
【问题讨论】:
-
欢迎来到 Stack Overflow!听起来您可能需要学习如何使用调试器来逐步执行代码。使用好的调试器,您可以逐行执行您的程序,并查看它与您期望的偏差在哪里。如果您要进行任何编程,这是必不可少的工具。进一步阅读:How to debug small programs.
-
在将
q->next指向任何东西之前,您先写q->next->n -
您需要提供相关的标题,以及“它不起作用”的意思
标签: c list pointers linked-list