【发布时间】:2015-05-15 07:58:14
【问题描述】:
我检查了其他类似问题的问题,但没有一个解决方案适用于我的情况。
手头的问题是,我正在尝试使用此结构创建具有动态内存的堆栈:
struct stekas{
int content;
struct stekas *link;
} *top = NULL;
但是,我在一些函数中遇到了麻烦:具体来说,“取消引用指向不完整类型的指针”。这是错误的代码:
struct node *temp;
temp = (struct stekas*)malloc(sizeof(struct stekas));
/* some code */
temp = top;
printf("Popped out number: %d\n", temp->content);
top = top->link;
free(temp);
这是另一个得到错误的函数:
int i;
struct node *temp;
/* some code */
for (i = top; i >= 0; i--) {
printf("%d\n", temp->content[i]);
我假设它与指针未连接到内容有关。我检查了其他问题,他们似乎对结构本身有问题,但我个人认为这个问题没有任何问题。
【问题讨论】:
-
标准警告:请do not cast
malloc()和C中的家人的返回值。 -
也许你想要
printf("%d\n", (temp[i]).content);而不是printf("%d\n", temp->content[i]);? -
您似乎认为
struct node包含一个名为content的int 数组,但您只向我们展示了struct steaks的定义
标签: c arrays struct compiler-errors dynamic-memory-allocation