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.
Related
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.
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.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
In order to understand tries I am creating this very simple C program that takes from user 10 nums from 0 to 9 as children of the trie. The final step is to print this nums with the function print, but I am getting a segmentation fault:
#include <stdio.h>
#include <stdlib.h>
typedef struct list
{
int data;
struct list *ar[10];
} list;
void insert(list *head);
void print(list *head);
int main(void)
{
printf("hello\n");
list *root = malloc(sizeof(list));
insert(root);
print(root);
}
void insert(list *head)
{
int a, i;
if (head == NULL) {
return;
}
for (i = 0; i < 10; i++) {
printf("Give Num 0-9\n");
scanf("%i", &a);
head->ar[a] = malloc(sizeof(head));
head = head->ar[a];
head->data = a;
}
}
void print(list *head)
{
if (head == NULL) {
return;
}
while (head != NULL) {
for (int i = 1; i < 10; i++) {
if (head->ar[i] != NULL) {
printf("%i", i);
head = head->ar[i];
break;
}
}
}
printf("\n");
}
There are several issues with your code:
The first mention of malloc doesn't actually initialize the memory (the ar field). You should initialize it properly
list *root = malloc(sizeof(list));
Is missing initialization, e.g.
root->data = 0;
for (size_t ii = 0; ii < 10; ii++) {
root->ar[ii] = NULL;
}
When you are actually gathering input, you allocate only enough memory for the pointer, not for the actual list itself.
head->ar[a] = malloc(sizeof(head));
should be initialized as above (head = malloc(sizeof(list)); for (size_t ...
There seems to be an infinite loop when actually running your program (after correcting all these issues).
EDIT: Remove calloc...
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.
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 8 years ago.
Improve this question
thanx k-ballo, u solved my prevoius problem but then i landed into another one!!
i created nodes and then tried to display them, but every time i called append_node(), the double pointer **head_ptr(used to hold the address of the very first pointer *head, which in turn, holds the address of the very first node of the linked list), was holding a NULL value as if previous calls to append_node(&head, value) didnt add any node to *head.
so whenever i display the list, it reamins empty!! :
#include <stdio.h>
struct __node
{ int data;
struct __node *next;
};
typedef struct __node node;
int append_node(node **head_ptr, int value) //double pointer head_ptr to simulate call-by-reference
{ node *temp, *q;
temp = (node *) malloc(sizeof(node));
if(!temp)
{ printf("\ninsufficient memory!!");
return -1;
}
q = *head_ptr; //as *head_ptr is address of a pointer (which is *head), so any changes made after this line in q should also be reflected in main().. (i guess so!)
temp->data = value;
temp->next = NULL;
if(q == NULL)
{ q = temp;
printf("\nq is empty");
return 0;
}
while( q->next != NULL)
{ q = q->next;
}
printf("\nq is not empty");
q->next = temp;
return 0;
}
int disp_list(node **head_ptr)
{ node *q;
int i=1;
q = *head_ptr;
if(q != NULL)
{ while( q != NULL )
{ printf("|%d-%d|--->", i++, q->data);
q = q->next;
}
}
else
{ printf("\nlist is empty!!");
}
return 0;
}
int main()
{ node *head=NULL;
int value, res, i=0;
while(i<3)
{ printf("\nenter the data to be inserted into the node: ");
scanf("%d", &value);
res = append_node( &head, value);
i++;
}
printf("\nprinting all the nodes...\n") ;
res = disp_list(&head);
printf("\n---------------\nexiting...\n\n\n");
return 0;
}
i know i could have returned *q from append_node() and reassigned it to *head or declared *head as global.. but i want to *head to be manipulated by so-called-pass-by-reference method only. (theres no pass-by-reference actually in c!) my compiler is: gcc version 4.5.2 (Ubuntu/Linaro 4.5.2-8ubuntu4)
plz help..i m not an expert so please use easier terminology!! :p
Your initial call to append_node is passing a pointer to a pointer to node which is uninitialized (let's pressume its null, though it will probably be just garbage). Then you do
q = *head_ptr;
//above statement causes a segment fault error..
// that statement should be fine, we will get the value of main's head, which we pressume to be null
// now we will try to dereference null by accesing its next element
while( q->next != NULL)
{ q = q->next;
}
There are two major problems with this program:
head is not initialized and contains garbage.
You don't handle the case of an empty list in append_node().
Both problems will lead to segmentation fault errors.