Expected identifier or ‘(’ before ‘char’ syntax error [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I can't find this syntax error that reads:
/home/ubuntu/workspace/stack.c:6:12: error: expected identifier or ‘(’ before ‘char’
char stack(char cmd[40])
^
The ^ symbol being under the second c in stack(char, I've looked around and I can't find an answer, most of them are something simple like a ; at the end of the main function, but I can't see anything wrong with this. Anyone got an idea?
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

You can't use #define stack and then later in the code, name a function stack. And you can't define functions inide a function.

#define stack
This line tells the precompiler to exchange the symbol stack with nothing.
The precompiler will process this line:
char stack(char cmd[40])
into this:
char (char cmd[40])
Without a function name, the code is no longer valid C.
I don't see why the #define is needed, so I would just remove it.

Related

passing dynamic array of structure to fun in c/c++ [duplicate]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am trying to compile my code and I am getting the Expected ';' identifier or '(' token before 'void' error on C:10:1.. can't seem to find the typo if someone can help me out I would appreciate it! I have provided my code down below I am sure it is just a silly mistake I made somewhere.
#include <stdio.h>
#include <stdlib.h>
//setting up my binary converter struct
struct bin
{
int data;
struct bin *link;
}
void append(struct bin **, int); //setting up append value
void reverse(struct bin**); //reverse values to convert to binary
void display(struct bin *); //so we can display
int main(void)
{
int nu,i; //global vars
struct bin *p;
p = NULL; //setting up p pointer to NULL to make sure it has no sense of being some other value
printf("Enter Value: ");
scanf("%d", &nu);
while (nu != 0)
{
i = nu % 2;
append (&p, i);
nu /= 2;
}
reverse(&p);
printf("Value in Binary: ");
display(p);
}
//setting up our append function now
void append(struct bin **q, int nu)
{
struct bin *temp,*r;
temp = *q;
if(*q == NULL) //making sure q pointer is null
{
temp = (struct bin *)malloc(sizeof(struct bin));
temp -> data = nu;
temp -> link = NULL;
*q = temp;
}
else
{
temp = *q;
while (temp -> link != NULL)
{
temp = temp -> link;
}
r = (struct bin *) malloc(sizeof(struct bin));
r -> data = nu;
r -> link = NULL;
temp -> link = r;
}
//setting up our reverse function to show in binary values
void reverse(struct bin **x)
{
struct bin *q, *r, *s;
q = *x;
r = NULL;
while (q != NULL)
{
s = r;
r = q;
q = q -> link;
r -> link = s;
}
*x = r;
}
//setting up our display function
void display(struct bin *q)
{
while (q != NULL)
{
printf("%d", q -> data);
q = q -> link;
}
}
You need to add semicolon (;) after declaration of your struct:
struct bin
{
int data;
struct bin *link;
};
Also, your main() function should return something. Add return 0; at the end of it.
And one more thing I noticed. Here:
int nu,i; //global vars
The comment does not make any sense, as nu and i are not global variables. They are local variables, because they are declared in the scope of your main() function.
EDIT:
The latter two comments obviously did not cause the compiling error, but I thought it would be a good think to mention them anyway.

Print a linked list in C

I am working to develop a linked list API for a Lisp compiler, but I chose to write this part in C because writing it in assembly wouldn't be very fun. I can successfully make a linked list from a series of elements with the list function below. I have verified that it works, shown by the examples in main below. But why does my function print_list only print the first element, and then zero? I am so confused. Please offer any help if you understand what is going wrong.
#include <stdio.h>
#include <stdarg.h>
typedef struct Node Node;
struct Node {
int head; // make to a union later
Node* tail;
};
//////////
Node cons(int curr, Node* next) {Node cell = {curr, next}; return cell;}
int car(Node node) {return node.head;}
Node* cdr(Node node) {return node.tail;}
//////////
void print_elems(Node node) {
printf("%d", node.head);
if (node.tail == NULL) return;
printf(" ");
print_elems(*node.tail);
}
void print_list(Node node) {
printf("(");
print_elems(node);
printf(")\n");
}
//////////
Node make_pairs(va_list args, int argc) {
Node linked_list;
linked_list.head = va_arg(args, int);
if (argc == 0) {
linked_list.tail = NULL;
return linked_list;
}
Node tail = make_pairs(args, argc - 1);
linked_list.tail = &tail;
return linked_list;
}
Node list(int length, ...) {
va_list elems;
va_start(elems, length);
Node linked_list = make_pairs(elems, length);
va_end(elems);
return linked_list;
}
//////////
int main() {
Node big_list = list(5, 1, 2, 3, 4, 5);
/*
This is NULL, as expected:
big_list.tail -> tail -> tail -> tail -> tail -> head
And this is 1:
big_list.head
*/
// but why does this only print "(1 0)"?
print_list(big_list);
}
You could print linked list in this kind of manner : this is an exemple;
My objective is just to make you notice with pointer you don't have problems with local variable
typedef struct Node Node;
Node* first_element = create_list; // just for understanding "first element" is the linked list
Node* ptr = first_element; // copy in another pointer your list then you will avoid loosing your first element and breaking your list (personnal advice : not needed)
while (ptr->tail){
ptrinf("%d\n", ptr->head); // for exemple
ptr = ptr->tail;
}
//after the while ptr = the last element because ptr->tail == NULL
formated to function it looks like that :
#include <stdio.h>
typedef struct Node Node;
void display_list (Node* first_element)
{
Node* ptr = first_element; // copy in another pointer your list then you will avoid loosing your first element and breaking your list (personnal advice : not needed)
while (ptr->tail){
ptrinf("%d\n", ptr->head); // for exemple
ptr = ptr->tail;
}
//after the while ptr = the last element because ptr->tail == NULL
}
if i didn't answer the question well tell me, i'm new on stack i would improve ^^.
I hope i helped you.

Expected ';' identifier or '(' token before 'void'? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am trying to compile my code and I am getting the Expected ';' identifier or '(' token before 'void' error on C:10:1.. can't seem to find the typo if someone can help me out I would appreciate it! I have provided my code down below I am sure it is just a silly mistake I made somewhere.
#include <stdio.h>
#include <stdlib.h>
//setting up my binary converter struct
struct bin
{
int data;
struct bin *link;
}
void append(struct bin **, int); //setting up append value
void reverse(struct bin**); //reverse values to convert to binary
void display(struct bin *); //so we can display
int main(void)
{
int nu,i; //global vars
struct bin *p;
p = NULL; //setting up p pointer to NULL to make sure it has no sense of being some other value
printf("Enter Value: ");
scanf("%d", &nu);
while (nu != 0)
{
i = nu % 2;
append (&p, i);
nu /= 2;
}
reverse(&p);
printf("Value in Binary: ");
display(p);
}
//setting up our append function now
void append(struct bin **q, int nu)
{
struct bin *temp,*r;
temp = *q;
if(*q == NULL) //making sure q pointer is null
{
temp = (struct bin *)malloc(sizeof(struct bin));
temp -> data = nu;
temp -> link = NULL;
*q = temp;
}
else
{
temp = *q;
while (temp -> link != NULL)
{
temp = temp -> link;
}
r = (struct bin *) malloc(sizeof(struct bin));
r -> data = nu;
r -> link = NULL;
temp -> link = r;
}
//setting up our reverse function to show in binary values
void reverse(struct bin **x)
{
struct bin *q, *r, *s;
q = *x;
r = NULL;
while (q != NULL)
{
s = r;
r = q;
q = q -> link;
r -> link = s;
}
*x = r;
}
//setting up our display function
void display(struct bin *q)
{
while (q != NULL)
{
printf("%d", q -> data);
q = q -> link;
}
}
You need to add semicolon (;) after declaration of your struct:
struct bin
{
int data;
struct bin *link;
};
Also, your main() function should return something. Add return 0; at the end of it.
And one more thing I noticed. Here:
int nu,i; //global vars
The comment does not make any sense, as nu and i are not global variables. They are local variables, because they are declared in the scope of your main() function.
EDIT:
The latter two comments obviously did not cause the compiling error, but I thought it would be a good think to mention them anyway.

Adding onto a linked list [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a .c and a .h file. All the linked list methods are supplied by my professor. I wanted to test the linked list out by creating a main function and trying to add to the linked list and then displaying the linked list. How would I do that in the main function? Here is what I have:
int main() {
linkedList* test = createLinkedList();
addToLinkedList(test, value);
displayLinkedList(test);
}
I also tried this code:
int main() {
linkedList* hello = createLinkedList();
struct tnode* test = "hello";
addToLinkedList(hello, test);
return (0);
}
However, what I have doesn't work.
Here is the code the prof gave us:
TESTlinkedlist.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "TESTlinkedList.h"
/* Creates a linked list */
linkedList* createLinkedList() {
return NULL;
}
/* Displays the linked list */
void displayLinkedList(linkedList* ll) {
linkedList* p = ll;
printf("[");
while (p != NULL) {
printf(" % d " , p -> node -> c);
p = p -> next;
}
printf("]\n");
}
/* Adds a tree node to the linked list */
void addToLinkedList(linkedList** ll, tnode* val) {
linkedList* nn = (linkedList*)malloc(sizeof(linkedList));
nn -> node = val;
nn -> next = NULL;
linkedList* p = *ll;
if (p == NULL) {
p = nn;
} else {
while (p -> next != NULL) {
p = p -> next;
}
p -> next = nn;
}
}
TESTlinkedlist.h:
/* Include Guards to prevent double inclusion with include directive */
#ifndef TESTLINKEDLIST_H
#define TESTLINKEDLIST_H
/* Typedef Structures */
typedef struct tnode {
double weight;
int c;
struct tNode* right;
struct tNode* left;
struct tNode* parent;
} tnode;
typedef struct ll {
tnode* node;
struct ll* next;
} linkedList;
/* Methods */
linkedList* createLinkedList();
void displayLinkedList(linkedList* ll);
void addToLinkedList(linkedList** ll, tnode* val);
void addInOrder(linkedList **ll, tnode* nn);
#endif /* LINKEDLIST_H */
Any clue how I can make a new linked list, create a t node and add it to that linked list, and then display it, given the methods that my prof put down?
The code your professor gave you is broken, for example struct tnode* test = "hello"; causes an error during compilation.
Take it back to him and fail him. While you're there, tell him return is not a function and he shouldn't cast malloc.
On another professor-malloc-related note, he should be checking the return value. For example:
linkedList* nn = malloc(sizeof *nn);
if (!nn) {
return;
}
nn -> node = val;
nn -> next = NULL;
There is another error that you're probably both at fault for; there are two main entry points and your compiler might get confused between them. Remove one of them.
In both of those entry points, addToLinkedList is used incorrectly; the first argument is intended to be a linkedList ** where-as what is being given is a linkedList *. Perhaps (in your first main entry point) you meant to write addToLinkedList(&test, value);. Note the additional ampersand.
the functions addToLinkedList and displayLinkedList need to get a pointer to a pointer as their fist parameter.
So, the main should look like this:
int main() {
linkedList* test = createLinkedList();
addToLinkedList(&test, value);
displayLinkedList(&test);
}
Hope this helps.

Passing argument from incompatible pointer type warning

I've been trying to figure out pointers in C most of today, even asked a question earlier, but now I'm stuck on something else. I've got the following code:
typedef struct listnode *Node;
typedef struct listnode {
void *data;
Node next;
Node previous;
} Listnode;
typedef struct listhead *LIST;
typedef struct listhead {
int size;
Node first;
Node last;
Node current;
} Listhead;
#define MAXLISTS 50
static Listhead headpool[MAXLISTS];
static Listhead *headpoolp = headpool;
#define MAXNODES 1000
static Listnode nodepool[MAXNODES];
static Listnode *nodepoolp = nodepool;
LIST *ListCreate()
{
if(headpool + MAXLISTS - headpoolp >= 1)
{
headpoolp->size = 0;
headpoolp->first = NULL;
headpoolp->last = NULL;
headpoolp->current = NULL;
headpoolp++;
return &headpoolp-1; /* reference to old pointer */
}else
return NULL;
}
int ListCount(LIST list)
{
return list->size;
}
Now in a new file I have:
#include <stdio.h>
#include "the above file"
main()
{
/* Make a new LIST */
LIST *newlist;
newlist = ListCreate();
int i = ListCount(newlist);
printf("%d\n", i);
}
When I compile, I get the following warning (the printf statement prints what it should):
file.c:9: warning: passing argument 1 of ‘ListCount’ from incompatible pointer type
Should I be worried about this warning? The code seems to do what I want it to, but I'm obviously very confused about pointers in C. After browsing questions on this site, I found that if I make the argument to ListCount (void *) newlist, I don't get the warning, and I don't understand why, nor what (void *) really does...
Any help would be appreciated, thanks.
You're getting confused because of multiple typedefs. LIST is a type representing a pointer to struct listhead. So, you want your ListCreate function to return a LIST, not a LIST *:
LIST ListCreate(void)
The above says: ListCreate() function will return a pointer to a new list's head if it can.
Then you need to change the return statement in the function definition from return &headpoolp-1; to return headpoolp-1;. This is because you want to return the last available head pointer, and you have just incremented headpoolp. So now you want to subtract 1 from it and return that.
Finally, your main() needs to be update to reflect the above changes:
int main(void)
{
/* Make a new LIST */
LIST newlist; /* a pointer */
newlist = ListCreate();
int i = ListCount(newlist);
printf("%d\n", i);
return 0;
}

Resources