I cannot understand why I keep getting the segmentation fault.
I use codeblocks and it compiles successfully, but the code doesn't execute. At the the time of execution, I keep getting this error.
This is my code:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *nc(int data)
{
struct node *temp = (struct node *)(malloc(sizeof(struct node)));
temp->data = data;
temp->next = NULL;
return temp;
}
int main()
{
struct node *head;
head->data = 5;
struct node *a = nc(19);
struct node *b = nc(25);
struct node *c = nc(12);
head->next = a;
a->next = b;
b->next = c;
while (head != NULL)
{
printf("%d \n", head->data);
head = head->next;
}
}
struct node *head;
"head" is NULL at this point.
You have to allocate memory like you did in the case of "temp".
struct node *head = (struct node *)(malloc(sizeof(struct node)));
Related
I am trying to add strings to a linked list but I have a problem with (null) being printed. Anyone who can help me?
The best I could do is narrow it down to this being the problem:
struct node *head = malloc(sizeof(struct node));
struct node *ptr = malloc(sizeof(struct node));
Here is the code:
#include <stdio.h>
#include <stdlib.h>
struct node {
char *data;
struct node *link;
};
struct node *add_begin(char *d, struct node *head) {
struct node *ptr = malloc(sizeof(struct node));
ptr->data = d;
ptr->link = head;
return ptr;
}
void add_end(struct node *point, char *data) {
struct node *temp = malloc(sizeof(struct node));
temp->data = data;
temp->link = NULL;
while (point->link != NULL) {
point = point->link;
}
point->link = temp;
}
int main() {
struct node *head = malloc(sizeof(struct node));
struct node *ptr = malloc(sizeof(struct node));
head->link = ptr;
char *data = "Chocolate Cake";
head = add_begin(data, head);
add_end(ptr, data);
while (head != NULL) {
printf("%s \n", head->data);
head = head->link;
}
}
Output:
Chocolate Cake
(null)
(null)
Chocolate Cake
The problem is you allocate dummy nodes, which are uninitialized and happen to have null pointers as data and link. The list should be initially empty, ie: head should be a null pointer.
Note that add_end should also return the head pointer in case an empty list was passed. Passing the arguments in the same order to both functions is highly recommended.
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
struct node {
char *data;
struct node *link;
};
struct node *add_begin(struct node *head, char *data) {
struct node *ptr = malloc(sizeof(*ptr));
ptr->data = data;
ptr->link = head;
return ptr;
}
struct node *add_end(struct node *head, char *data) {
struct node *ptr = malloc(sizeof(*ptr));
ptr->data = data;
ptr->link = NULL;
if (head == NULL) {
return ptr;
} else {
struct node *temp = head;
while (temp->link != NULL) {
temp = temp->link;
}
temp->link = ptr;
return head;
}
}
int main() {
struct node *head = NULL;
char *data = "Chocolate Cake";
head = add_begin(head, data);
head = add_end(head, data);
for (struct node *ptr = head; ptr; ptr = ptr->link) {
printf("%s\n", ptr->data);
}
return 0;
}
Output:
Chocolate Cake
Chocolate Cake
I'm new to C and this language is confusing me a bit.
I keep getting a segmentation fault 11 when running my quite simple linked list code:
struct node{
int val;
struct node *next;
};
struct node *init(){
struct node *l = NULL;
return l;
}
struct node *newNode(int val){
struct node* n = init();
n = (struct node*) malloc(sizeof(struct node));
n->val = val;
n->next=NULL;
return n;
}
void append(struct node* h, int val){
struct node *temp;
temp = h;
int i = 0;
while(temp->next != NULL){
temp = temp->next;
i++;
}
printf("TestAppend");
temp= newNode(val);
}
int main(){
struct node* l = init();
printf("Test1\n");
append(l, 15);
printf("Test2\n");
struct node* temp = init();
temp = l;
}
can someone please explain why?
Thanks :)
Try to change init function to:
struct node *init()
{
void* p = malloc(sizeof(struct node));
return (struct node*)p;
}
Edit:
and free it at the end! – Woodrow Barlow
The segmentation fault is because your code is pointing to NULL.
If you replace calls to init() with simple NULL (which is what it is), you will see for yourself that append tries to use NULL->next.
In append, you should check for 'temp!=NULL' first to avoid the seg fault. Also you are declaring an unnecesary local variable which you are not using at all.
I'm writing a program that creates a doubly linked list out of an array. Here's the code so far:
#include<stdio.h>
#include<stdlib.h>
struct Node {
int data;
struct Node *next;
struct Node *previous;
}
struct Node *create_dll_from_array(int array[], int x) {
int i;
struct Node *newNode, *temp, *head;
for (i=0; i<x; i++) {
newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = *(array+i);
if (i=0) {
head = newNode;
temp = newNode;
newNode->next = NULL;
newNode->previous = NULL;
}
else {
*** temp->next = (struct Node*) newNode->data;
newNode->next = NULL;
*** newNode->previous = (struct Node*) temp->data;
temp = newNode;
}
}
return head;
}
int main(){
int array[5] = {11,2,7,22,4};
struct Node* head;
head = create_dll_from_array(array,5);
return 0;
}
So in the lines with ***, I'm getting the error: warning: cast to pointer from integer of different size
I don't know whether the program itself actually works, just asking about those two lines and why they aren't working. Thanks!
How to fix casting a pointer to an integer?
Don't assign an int to a pointer and then the need for casting is gone.
Assign a pointer to a pointer.
// temp->next = (struct Node*) newNode->data;
temp->next = newNode;
You can improve on these things:
Always initialize your pointer with NULL. This will guard you against the pointer pointing to an invalid address.
Do not hard code the array size value. Calculate it.
In the if condition you have used assignment =. Change that to equality check ==. If you will not do this, your program will crash.
just asking about those two lines and why they aren't working
It is because, temp->next points to a memory location of type struct node. You cant assign it an integer value (as you were doing). I have reproduced your full code below some of your lines commented.
#include<stdio.h>
#include<stdlib.h>
struct Node {
int data;
struct Node *next;
struct Node *previous;
};
struct Node *create_dll_from_array(int array[], int x) {
int i;
// struct Node *newNode, *temp, *head;
struct Node *newNode= NULL, *temp=NULL, *head=NULL;
for (i=0; i<x; i++) {
newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = *(array+i);
// if (i=0) { Its wrong
if (i==0) {
head = newNode;
temp = newNode;
newNode->next = NULL;
newNode->previous = NULL;
}
else {
// temp->next = (struct Node*) newNode->data; // issue
temp->next = (struct Node*) newNode;
newNode->next = NULL;
// newNode->previous = (struct Node*) temp->data; //issue
newNode->previous = (struct Node*) temp; //issue
temp = newNode;
}
}
return head;
}
int main(){
// int array[5] = {11,2,7,22,4};
int array[] = {11,2,7,22,4};
struct Node* head;
// head = create_dll_from_array(array,5);
head = create_dll_from_array(array,sizeof(array)/sizeof(*array));
return 0;
}
Few more optimization that you can do is that inside your create_dll function, the if condition is hit only for the first time. You can move that to else condition and make your else condition your if condition
I am trying to create a simple linked list using C, I think I was able to construct the linked list itself, but when I try and print it, it prints the value of the last node instead of all the values in the list.
#include <stdio.h>
#include <alloca.h>
typedef int DATA;
struct Node
{
DATA d;
struct Node *next;
};
void printList(struct Node **head)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
struct Node *temp;
temp = *head;
while(temp!=NULL)
{
printf("%d \n", temp->d);
temp = temp->next;
}
}
int main()
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
struct Node *head = newNode;
struct Node *temp = newNode;
head->d = 1;
int i = 0;
printf("Enter 3 numbers");
for( i = 0; i< 3; i++)
{
scanf("%d", &temp->d);
temp->next = newNode;
temp = temp->next;
}
temp->next = NULL;
printf("%d \n", temp->d);
return 0;
}
Any help/tips would be greatly appreciated.
There are multiple problems in your code:
You are not creating the list properly. You are allocating memory for only one node and playing around with pointers improperly causing the linked list to be pointed to the same memory
You are not calling printList function at all
It is not clear why you are allocating memory again in printList whereas it should be printing the already created list
Also, no need to pass double pointer for printList as you are not modifying the list.
Though you should be understanding and doing the changes yourself, I am providing a below modified program which I hope will help you understand the mistakes in your code.
Please see the below modified version of the code
#include<stdio.h>
#include <alloca.h>
typedef int DATA;
struct Node
{
DATA d;
struct Node *next;
};
void printList(struct Node *head)
{
struct Node *temp = head;
while(temp!=NULL)
{
printf("%d \n", temp->d);
temp = temp->next;
}
}
struct Node *createNode()
{
struct Node *newNode;
newNode = malloc(sizeof(struct Node));
if (NULL == newNode)
return NULL;
memset(newNode, 0, sizeof(struct Node));
return newNode;
}
int main()
{
struct Node *newNode = NULL;
struct Node *headNode = NULL;
struct Node *temp = NULL;
int i = 0;
int data = 0;
printf("Enter 3 numbers\n");
for( i = 0; i< 3; i++)
{
scanf("%d", &data);
newNode = createNode();
if (NULL == newNode)
break;
newNode->d = data;
if (headNode == NULL)
{
headNode = newNode;
temp = newNode;
}
else
{
temp->next = newNode;
temp = temp->next;
}
}
printList(headNode);
return 0;
}
Replace your code with the following code :-
#include<stdio.h>
#include <alloca.h>
typedef int DATA;
struct Node
{
DATA d;
struct Node *next;
};
void printList(struct Node **head)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
struct Node *temp;
temp = *head;
while(temp->next!=NULL)
{
printf("%d \n", temp->d);
temp = temp->next;
}
}
int main()
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
struct Node *head = newNode;
struct Node *temp = newNode;
head->d = 1;
int i = 0;
printf("Enter 3 numbers");
for( i = 0; i< 3; i++)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
scanf("%d", &temp->d);
temp->next = newNode;
temp = temp->next;
}
printList(head);
return 0;
}
Here you need to declare the newNode in the loop also while taking the input. As, the current value is over-writted, the old value is lossed and only the value of last node is printed.
Also while printing, check for temp->next!=Null instead of temp!=NULL
#include<stdio.h>
#include<malloc.h>
typedef struct Node {
int data;
struct Node * next;
} Node;
void push(Node **headRef, int i){
//why does headRef == NULL in below if condition gives segmentation fault?
if(*headRef == NULL){
*headRef = malloc(sizeof(Node));
Node *head = *headRef;
head->data = i;
}
}
int main(int argc, char ** argv){
Node *head = NULL;
push(&head, 2);
printf("%d\n", head->data);
}
This code is of linked list where I try to push some data into the list.
My question is in the comment of push function.
No need for the test. If *headRef happens to be NULL, newnode->next will be set to NULL, otherwise to *headRef.
void push(Node **headRef, int i){
Node *new;
new = malloc(sizeof *new);
/* check for new==NULL omitted */
new->next = *headRef;
new->data = i;
*headRef = new;
}
Yes, segfault is later at head->data access (if you use headRef==NULL)