我找不到这个语法错误:
/home/ubuntu/workspace/stack.c:6:12:错误:预期标识符或'(''char'之前' char stack(char cmd [40])
^
^符号位于堆栈中的第二个c下(char,我环顾四周,我找不到答案,大多数都是简单的东西;在主函数的末尾,但我可以看不出有什么问题。有人有个主意吗?
stack.c
#ifndef stack
#define stack
#include <stdio.h>
#include "stack.h"
char stack(char cmd[40])
{
stacks newstack()
{
stacks s;
s -> head = NULL;
return s;
}
void deletestack(stacks s)
{
node temp;
while(s -> head)
{
temp = s -> head;
s -> head = s -> head -> next;
free(temp);
}
free(s);
}
int isEmpty(stacks s)
{
if(s -> head == NULL)
return 1;
else
return 0;
}
void push(stacks s, element e)
{
node n = (node)malloc(sizeof(node_type));
n -> e = e;
n -> next = s -> head;
s -> head = n;
}
element peek(stacks s)
{
return s -> head -> e;
}
void display(stacks s)
{
while(s -> head)
{
printf("%d\n", s -> head -> e);
}
}
element pop(stacks s)
{
printf("%d\n", s -> head -> e);
temp = s -> head;
s -> head = s -> head -> next;
free(temp);
}
}
#endif
stack.h
#ifndef ____Linked_List_H____
#define ____Linked_List_H____
#include "stdheader.h"
//Structures
//element is content of a node.
typedef int element;
//node is 1 link in a linked list.
struct _node{
element e;
struct _node* next;
};
typedef struct _node node_type;
typedef struct _node* node;
//linked list is a series of links tracked by the head or start of the list. struct _linked_list{
node head;
};
typedef struct _linked_list stacks_type;
typedef struct _linked_list* stacks;
stacks newstack();
void deletestack(stacks);
int isEmpty(stacks);
element pop(stacks);
void push(stacks, element);
element peek(stacks);
void display(stacks);
#endif
答案 0 :(得分:2)
您不能使用#define堆栈,然后在代码中命名函数堆栈。而且您无法在函数内定义函数。
答案 1 :(得分:0)
#define stack
该行告诉预编译器将符号stack
与任何内容交换。
预编译器将处理这一行:
char stack(char cmd[40])
进入这个:
char (char cmd[40])
没有函数名称,代码不再有效C.
我不明白为什么需要#define
,所以我会删除它。