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.
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 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)
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
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 am trying to append a node at the end of the linked list and I am getting a segmentation fault. I am not able to figure out where is my mistake. Any Help and suggestions would be appreciated!
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
}*head;
void append(int x)
{
struct node *temp1,*right;
temp1=malloc(sizeof(struct node));
temp1->data=x;
right=malloc(sizeof(struct node));
right=head;
while(right->next != NULL )
right=right->next;
right->next=temp1;
right=temp1;
right->next=NULL;
}
void print(){
struct node *temp=head;
printf("List is: ");
while( temp!=NULL )
{
printf(" %d",temp->data);
temp=temp->next;
}
printf("\n");
}
int main(){
struct node *temp;
int n,i,x;
head=NULL;//empty list;
printf("how many numbers?\n");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("\nEnter the number\n");
scanf("%d",&x)
append(x);
print();
}
}
In your function append you dereference a pointer which points to NULL-
right=malloc(sizeof(struct node));
right=head;
while(right->next != NULL )
....
As head points to NULL and then you point to head by right , so basically by right->next you dereference a pointer to NULL . That's why you maybe getting a seg fault .
And also you allocate memory to right and loose reference to it after making it to point head causing memory leak (avoid such things).
This can be avoided by pointing right after allocating memory by head -
head=right;
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;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
struct node{
int data;
struct node *next;
};
struct node *head,*temp;
void insert()
{
struct node *var;
head=NULL;
var=(struct node*)malloc(sizeof(struct node));
printf("enter the data:");
scanf("%d",var->data);
temp=head;
if(head==NULL)
{
head=var;
head->next=NULL;
}
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
if(temp->next==NULL)
{
temp->next=var;
temp=temp->next;
temp->next=NULL;
}
}
}
void display()
{
temp=head;
if(temp==NULL)
{
printf("empty list");
}
while(temp->next!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
}
}
void main()
{
int value,choice;
printf("\nenter choice:");
scanf("%d",&choice);
while(choice==1)
{
insert();
display();
printf("\nenter choice:");
scanf("%d",&choice);
}
getch();
}
i have made a linklist using c above but the code is not displaying the linklist,instead it shows null pointer compilattion as output,how to solve the problem,i'm new to c coding so cant find adequate solution for this ????
scanf("%d",var->data);
//--> scanf("%d",&(var->data));
scanf's argument must be pointer type.
With every insert, you reset head to NULL. So you will always insert new values at the head and any existing values will be just left in memory, leading to a memory leak.
I guess you want to move the line head=NULL; to the beginning of the main method.
And fix your scanf like keeptalk said.
You seem to be missing to initialise *var's member next after having allocated *var using malloc().