c - 使用链表的 C 堆栈中的预期标识符

标签 c compiler-errors

我在尝试编译代码时遇到了几个错误。我的代码表示使用 LinkedList 和指针的堆栈。错误被标记为注释。我跳过了几个方法,因为错误发生在方法“创建新元素”

这是错误:

In function ‘new_item’:
framework_stack.c:36:9: error: expected identifier or ‘(’ before ‘->’ token
     item->info = value;
     ^
framework_stack.c:37:9: error: expected identifier or ‘(’ before ‘->’ token
 item->ptr = NULL;
     ^

代码:

#include <stdio.h>
#include <stdlib.h>

struct node  
{
    int info;
    struct node *ptr;
}*top,*top1,*temp;

typedef struct node item;

item *top = NULL;

// my methods
void push(item *elem);
void *pop();
void empty();
void create();

int count = 0;

void create()
{
  top = NULL;
}

/* Creating new element */
item* new_item(int value) 
{ 
    item *temp = malloc(sizeof(item));
    item->info = value; // HERE IS THE ERROR
    item->ptr = NULL;  // HERE IS THE ERROR
}

void push(item *elem)
{
    if (top == NULL)
    {  
        top = elem;
    }
    else
    {
        top->ptr = elem;
        top = elem;
    }
    count++;
    item* head = NULL;

最佳答案

item 是一种类型,而不是变量。

使用

  temp->info = value;

关于c - 使用链表的 C 堆栈中的预期标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33801061/

相关文章:

c - 如何检查 typedef 结构的内部 typedef 结构是否为 NULL?

c++ - 无法将类型转换为类型* - C++ 编译错误

c - 如何计算 Linux 中进程中线程使用的 CPU 百分比?

c - 如何在C语言中以指数形式(即6.6.7e-11)获得计算答案

ios - Swift:EGODataBaseRowResult 没有名为 Generator 的成员

c++ - boost 几何: assertion_failed error C++

c++ - 将 vector<double> 插入 vector<vector<double>>

c - 在 Windows 上从源代码构建 PHP 时,类似的语法会导致奇怪的重复编译错误

c - 优化 C 语言中近似欧拉数 (e) 的算法

c - strcat 和静态分配的字符数组会发生什么情况?