Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Question

Post Timeline

I trytried to define a node struct which includes a node* next. I write a append(node* n)an append(node* n) function to add a new node next to the previous one, but every time I run the code, it gives me seg faulfault. My code is following:

#include<stdlib.h>
#include<stdio.h>
typedef struct _log_t {
 struct _log_t* next;
}log_t;
void initi(log_t* l) {
 l = (log_t*)malloc(sizeof(log_t));
 l -> next = NULL;
}
void append(log_t* l){
 l->next = (log_t*)malloc(sizeof(log_t)); 
 l->next->next = NULL;
 l = l->next;
}

Thanks in advance for any help!

I try to define a node struct which includes a node* next. I write a append(node* n) function to add a new node next to the previous one, but every time I run the code, it gives me seg faul. My code is following:

#include<stdlib.h>
#include<stdio.h>
typedef struct _log_t {
 struct _log_t* next;
}log_t;
void initi(log_t* l) {
 l = (log_t*)malloc(sizeof(log_t));
 l -> next = NULL;
}
void append(log_t* l){
 l->next = (log_t*)malloc(sizeof(log_t)); 
 l->next->next = NULL;
 l = l->next;
}

Thanks in advance for any help!

I tried to define a node struct which includes a node* next. I write an append(node* n) function to add a new node next to the previous one, but every time I run the code, it gives me seg fault. My code is following:

#include<stdlib.h>
#include<stdio.h>
typedef struct _log_t {
 struct _log_t* next;
}log_t;
void initi(log_t* l) {
 l = (log_t*)malloc(sizeof(log_t));
 l -> next = NULL;
}
void append(log_t* l){
 l->next = (log_t*)malloc(sizeof(log_t)); 
 l->next->next = NULL;
 l = l->next;
}

Thanks in advance for any help!

Source Link
Shen
  • 3.2k
  • 3
  • 17
  • 10

pointer malloc fail

I try to define a node struct which includes a node* next. I write a append(node* n) function to add a new node next to the previous one, but every time I run the code, it gives me seg faul. My code is following:

#include<stdlib.h>
#include<stdio.h>
typedef struct _log_t {
 struct _log_t* next;
}log_t;
void initi(log_t* l) {
 l = (log_t*)malloc(sizeof(log_t));
 l -> next = NULL;
}
void append(log_t* l){
 l->next = (log_t*)malloc(sizeof(log_t)); 
 l->next->next = NULL;
 l = l->next;
}

Thanks in advance for any help!

lang-c

AltStyle によって変換されたページ (->オリジナル) /