Expected declaration specifiers [closed] - c

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
I am learning linked lists in C. I am new to using pass by references for manipulating the linked lists. Now I know I'm doing something really foolish in this program. This program creates a list and then basically returns the number of instances of a particular value(the data of node). I get an error like so, "Expected declaration specifier" before every statement of main!.
What's going on wrong?
#include<stdio.h>
#include<malloc.h>
struct list {
int number;
struct list *next;
};
typedef struct list node;
void create(node *);
int count(node **,int);
main()
int key,this_many;
node *head;
head = (node *)malloc(sizeof(node));
create(head);
printf("Which number?\n");
scanf("%d",&key);
this_many = count(&head,key);
printf("%d times\n",this_many);
return 0;
}
void create(node *list) {
printf("Enter a number -999 to stop\n");
scanf("%d",&list->number);
if(list->number == -999) {
list->next = NULL;
}
else {
list->next = (node *)malloc(sizeof(node));
create(list->next);
}
}
int count(node **addr_list,int key) {
int count = 0;
while(*addr_list != NULL) {
if((*addr_list)->number == key) {
count++;
}
*addr_list = (*addr_list)->next;
}
return(count);
}

Problems:
You are not specifying the return type of main.
You don't have the { to start the scope of main.
Change the lines starting with main to:
int main()
{
int key,this_many;
node *head;
head = (node *)malloc(sizeof(node));
create(head);
printf("Which number?\n");
scanf("%d",&key);
this_many = count(&head,key);
printf("%d times\n",this_many);
return 0;
}

Related

Linked List remove node function not working properly [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 8 months ago.
Improve this question
When I run the program below - the output I get is
head node created
node addded with value of 22
node addded with value of 22343
node addded with value of 7
22
22343
7
last node has been removed
current last nodes value is 22343
22
22343
7
This issue is that , if the last node in the list is stated to have a data value of 22343 when the removenode function is called , how is it possble that the last value printed is 7 and not 22343 - when the traverse function is called? despite the fact that the *next pointer of the last node is set to NULL in the removenode funtion.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node_n
{
int value;
struct node *next;
};
typedef struct node_n node;
node* makeheadnode()
{
node *temp = (node*)malloc(sizeof(node));
temp->next = NULL;
temp->value = NULL;
printf("\n head node created");
return temp;
}
void addnode(node *target, int data)
{
while (target->next != NULL)
{
target = target->next;
}
target->next = (node*)malloc(sizeof(node));
target = target->next;
target->value = data;
target->next = NULL;
printf("\n node addded with value of %d ",target->value);
}
int removenode(node *target)
{
node *temp;
if(target->next == NULL)
{
printf("\n only one node is present");
return 1;
}
while(target -> next != NULL)
{
temp = target -> next;
if(temp -> next == NULL)
{
target->next == NULL;
printf("\n last node has been removed \n current last nodes value is %d",target->value);
return 1;
}
else
{
target = target-> next;
}
}
}
int traverse(node *target)
{
if(target->next == NULL)
{
printf("\n this list is empty");
return 1;
}
while (target->next != NULL)
{
target = target -> next;
printf("\n %d", target -> value);
}
return 1;
}
int main()
{
node *head = makeheadnode();
addnode(head,22);
addnode(head,22343);
addnode(head,007);
traverse(head);
removenode(head);
traverse(head);
}
The problem is a typo. In your removenode function, you have:
target->next == NULL;
Where it should be:
target->next = NULL;
If you compile with warnings on, i.e. -Wall or even -Werror, the compiler should have warned you with a message like equality comparison result unused.
Furthermore, since you're removing the node, you should free it so you don't have any memory leaks. Setting a pointer to NULL does not free the pointer. Many, including myself, also see it as good practice to set the pointer to NULL after freeing.
free(target->next)
target->next = NULL
For more info on that, see this post

Upon creation of 3 - 5 nodes, segmentation fault is encountered. Why? [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 last year.
Improve this question
I am currently trying to create a phonebook with a linked list. I am focusing first on the insertion function but the problem is that once I create 3 - 5 nodes, onlineGDB shows the error "malloc : corrupted top size", and VS Code shows that I got a segmentation error.
I assume this is an error with the way I am allocating memory. This is the first time that I am working on a structure that contains strings as data instead of integer so I might have missed a thing or two.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node{
char name[30];
char number[15];
struct node *next;
};
void showMenu();
void insertData(struct node **head, char person[], char phone[]);
void showData(struct node *head);
int main(){
struct node *head = NULL;
char name[30], number[15];
while(1)
{
printf("Name : ");
scanf(" %[^\n]s", name);
printf("Number: ");
scanf(" %[^\n]s", number);
insertData(&head, name, number);
showData(head);
}
return 0;
}
void insertData(struct node **head, char person[], char phone[]){
struct node *new_node = (struct node*) malloc(sizeof(struct node*));
strcpy(new_node->name, person);
strcpy(new_node->number, phone);
new_node->next = *head;
*head = new_node;
}
void showData(struct node *head){
while(head != NULL)
{
printf("%s\t\t%s\n", head->name, head-> number);
head = head->next;
}
printf("\n");
}
Use malloc(sizeof(struct node)) instead of malloc(sizeof(struct node*)) in your function insertData(). You want to allocate the size of the node and not the pointer to node.

My code for inserting numbers to the start of linked list doesn't compile [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 1 year ago.
Improve this question
here's the code
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node* next;
}node;
node* head;// global variable can be accessed anywhere
void Insert(int x);//function to insert the number in start
void print();//function to print them
int main()
{
head = NULL;// empty list
printf("How many numbers?\n");
int n,i,x;
scanf("%i", &n);
for(i = 0; i < n; i++)
{
printf("Enter the Number\n");
scanf("%i", &x);
Insert(x);
}
print();
}
void Insert(int x)
{
node* temp = malloc(sizeof(node));
temp->data = x;
temp->next = head;
head = temp;
}
void print()
{
node* temp = head;
print("List is: ");
while(temp != NULL)
{
printf("%d ",temp->data);
temp = temp->next;
}
printf("\n");
}
so I tried to compile the code in VS Code & it asks for the How many numbers, then asks for to enter the number & after entering the 1st number, the program stops.
so I tried to compile in CS50 ide & this is the error I got-
linkedlist1.c:41:1: error: all paths through this function will call itself [-Werror,-Winfinite-recursion]
{
^
Line 41 is 1st curly bracket of the function void print()
why is this happening? & how should I solve it?
Calling
print("List is: ");
inside the function
void print()
unconditionally is an infinite recursion.
You may meant
printf("List is: ");
(call printf with f, not print)
To avoid this kind of error, you should mark functions that doesn't take arguments not to take arguments explicitly. This can be done by using void as the arguments like
void print(void)

i'm trying to print linked list reverse via recursion but it doesn't work [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 tried to print linked list in reverse order via recursion.
this is code:
#include <stdio.h>
#include <stdlib.h>
typedef struct list {
int value;
struct list* next;
} list_s;
static int g_size=sizeof(list_s);
void printRe(list_s *node) {
if (node = NULL)
return;
printRe(node->next); // this is where error happens
printf("%d", node->value);
}
int main() {
list_s *head;
head = (list_s*)malloc(g_size);
int n;
scanf("%d",&n);
int i = 0;
int x;
scanf("%d", &x);
head->value = x;
head->next = NULL;
list_s *current;
current = head;
for (i; i < n - 1; ++i) {
current->next = (list_s*)malloc(g_size);
current = current->next;
scanf("%d", &x);
current->value = x;
current->next = NULL;
};
printRe(head);
return 0;
}
So as you see, the error occurs when I tried printing node->next. why did error occur? did i passed list to a function wrong way?
Thank you!
if condition in printRe() function should be like
if(node==NULL) instead of if(node=NULL)
i hope it will help you.
Thanks

linked list remains empty even after adding nodes? [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 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.

Resources