I know this question has been asked numerous times previously, however I am racking my brain trying to figure out what is causing the error as I cannot identify where the usual missing semi-colon is.
<#include<stdio.h>
#define MaxLenght 30
typedef int ElementType;
typedef int Position
typedef struct {
ElementType Elements[MaxLength];
Position Last;
} List L;
void MAKENULL_LIST(List *L){
L->Last=0;
}
Position First(List L) {
return 1;
}
Position EndList(List L) {
return L.Last+1;
}
ElementType Retrieve(Position P, List L) {
return L.Elements[P-1];
}
Position Next(Position P, List L) {
return P+1;
}
void printList(List L) {
Position P;
P = First(L);
while (P != EndList(L)) {
printf("%d ",Retrieve(P,L));
P = Next(P, L);
}
printf("\n");
}
Error thrown by compiler:
__tester__.c:26:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token } List *L;
You have several typos:
1 Replace:
#define MaxLenght 30
By
#define MaxLength 30
2 Replace:
typedef int Position
by
typedef int Position;
3 Replace:
} List L;
by
} List;
And voilà, it compiles.
Related
Yes, I know this question has been asked numerous times previously, however I am racking my brain trying to figure out what is causing the error as I cannot identify where the usual missing semi-colon is.
list.h
/* list.h */
#ifndef _LIST_H
#define _LIST_H
#define TRUE_ true
#define FALSE_ false
class LISTElement {
public:
LISTElement(void *itemPtr, int sortKey); /* initialize a list element */
LISTElement *next; /* next element on list,
NULL if this is the last */
int key; /* priority, for a sorted list */
void *item; /* pointer to item on the list */
};
class LIST {
public:
LIST(); /* initialize the list */
~LIST(); /* de-allocate the list */
bool IsEmpty(); /* is the list empty? */
void Insert(void *item, int sortKey); /* Put item into list */
void *Remove(int *keyPtr); /* Remove first item from list */
private:
LISTElement *first; /* Head of the list, NULL if list is empty */
LISTElement *last; /* Last element of list */
};
#endif /* _LIST_H */
list.c
#include <stdlib.h>
#include "list.h"
LISTElement::LISTElement(void *itemPtr, int sortKey) {
item = itemPtr;
key = sortKey;
next = NULL; /* assume we'll put it at the end of the list */
}
LIST::LIST() {
first = last = NULL;
}
LIST::~LIST() {
while (Remove(NULL) != NULL)
; // delete all the list elements
}
bool LIST::IsEmpty() {
if (first == NULL)
return TRUE_;
else
return FALSE_;
}
void LIST::Insert(void *item, int sortKey) {
LISTElement *element = new LISTElement(item, sortKey);
LISTElement *ptr;
if (IsEmpty()) { /* if list is empty, put */
first = element;
last = element;
} else if (sortKey < first->key) {
/* item goes on front of list */
element->next = first;
first = element;
} else { /* look for first element in list bigger than item */
for (ptr = first; ptr->next != NULL; ptr = ptr->next) {
if (sortKey < ptr->next->key) {
element->next = ptr->next;
ptr->next = element;
return;
}
}
last->next = element; /* item goes at end of list */
last = element;
}
}
void * LIST::Remove(int *keyPtr) {
LISTElement *element = first;
void *thing;
if (IsEmpty())
return NULL;
thing = first->item;
if (first == last) { /* list had one item, now has none */
first = NULL;
last = NULL;
} else {
first = element->next;
}
if (keyPtr != NULL)
*keyPtr = element->key;
delete element;
return thing;
}
Error thrown by compiler:
$ gcc -o list list.c
In file included from list.c:2:
list.h:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘LISTElement’
list.h:18: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘LIST’
list.c:4: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘:’ token
list.c:10: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘:’ token
list.c:14: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘:’ token
list.c:19: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘LIST’
list.c:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘:’ token
list.c:50: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘:’ token
This time it's not a missing semicolon, but a completely different language. The code you're trying to compile is in C++, not C. That's why you get syntax errors from a C compiler.
To fix this (and compile the code as C++), rename the file from .c to .cpp:
mv list.c list.cpp
And use g++ to compile it, not gcc:
g++ -o list list.cpp
My code:
#include <stdio.h>
node * create(int);
void disp(node *,int);
typedef struct node
{
int data;
struct node *next;
};
node * create(int);
void disp(node *,int);
typedef struct node *head , *p , *c;
int i,n;
int main()
{
printf("\n Enter the number of nodes:");
scanf("%d",&n);
c=create(n);
disp(head,n);
return 0;
}
node * create(int n)
{
head = (node *)malloc(sizeof(node));
scanf("%d", &head->data);
head->next = NULL;
p=head;
for(i=1;i<n;i++)
{
p=(node*)malloc(sizeof(node));
scanf("%d",&p->data);
p=p->next;
p->next=NULL;
}
return head;
}
void disp(node *head , int n)
{
p=head;
while(p!=NULL)
{
for(i=0;i<n;i++)
{
printf("%d",p->data);
p=p->next;
}
}
}
OUTPUT:
User#Sujata:~/Desktop]$ gcc ll.c
ll.c:3: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
ll.c:4: error: expected ‘)’ before ‘*’ token
ll.c:10: warning: useless storage class specifier in empty declaration
ll.c:11: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
ll.c:12: error: expected ‘)’ before ‘*’ token
ll.c: In function ‘main’:
ll.c:19: error: expected identifier or ‘(’ before ‘=’ token
ll.c:20: error: expected expression before ‘head’
ll.c: At top level:
ll.c:24: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
ll.c:41: error: expected ‘)’ before ‘*’ token
Got this output . Tried many times using the typedef keyword also. But does not work . Thanks in advance !
.....Your code is messy......
You need to add
typedef struct node node;
before the forward declaration of functions so that the type node is known by the compiler. Also, remove the typedef from
typedef struct node *head , *p , *c;
I have the following code:
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
#define MAXSIZE 1024
typedef struct stack{
struct TreeNode volum[MAXSIZE];
int top;
}STACK;
STACK st;
st.top = -1;
/* other function definitions such as pop() and push() */
But when I compile it, it gives me error Line 18: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token. Where Line 18 is st.top=-1;
So here I need to initialize the stack, i.e setting the top to -1. I also tried to do it inside the struct: int top=-1; But got the same error. I wonder what the correct way of doing it is. Thanks in advance.
You could try
typedef struct stack {
int top;
struct TreeNode volum[MAXSIZE];
} STACK;
STACK st = { -1 }; // top has to be the first member of the struct
Put st.top = -1; in some function (e.g. main), because you can't do assigning globally.
The compiler is gcc and I'm using an old version of linux
typedef struct strlist strlist;
struct strlist
{
char *data;
time_t *timestamp;
struct strlist *next;
}
strlist * list_directory(char *dirname)
{
//do something
}
The error message is:
error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
and it's on the line:
strlist * list_directory(char *dirname)
struct strlist
{
char *data;
time_t *timestamp;
struct strlist *next;
};
^^^
|
You are missing the ; at the end of structure declaration.
You need to end the struct declaration with a semicolon. That tripped me up so many times when I started programming in C...
I am trying to write code to build a stack, but I'm getting compilation errors which don't make sense to me. Here is my stack.h:
struct StackNode {
void* previous;
int value;
};
struct Stack {
StackNode* top;
};
Stack* new_stack () {
StackNode stn = { NULL, 0 };
Stack* st = (Stack*) malloc(sizeof(Stack));
st->top = NULL;
return st;
}
and my main.c:
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
int main () {
struct Stack* st = new_stack();
return 0;
}
gcc throws these errors:
make (in directory: /home/diego/temp/stack) gcc -g -O2 -std=c99 -c
main.c In file included from main.c:4: Compilation failed. stack.h:10:
error: expected specifier-qualifier-list before ‘StackNode’
stack.h:14: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’
before ‘*’ token main.c: In function ‘main’: main.c:8: warning:
implicit declaration of function ‘new_stack’ main.c:8: warning:
initialization makes pointer from integer without a cast make: *
[main.o] Error 1
EDIT: I found the error. I forgot to put struct before Stack and StackNode in some lines. Always having struct on those lines solves the issue.
Change:
struct Stack {
StackNode* top;
};
to:
struct Stack {
struct StackNode* top;
};
and anywhere else StackNode or Stack is used and not preceded by struct. If you wish to not specify struct you could use a typedef.