ポインタ変数がNULL
かどうかをチェックしようとするたびに、このセグメンテーションフォルトが得られました。エラーは、追加機能のコードのこれらの行から次のとおりです。C - ポインタがNULLかどうかをチェックする際のセグメンテーションエラー
if (it->head == NULL){
printf("worksfine");
}
これは私が持っている全体のコードです:
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef struct Node{
int val;
struct Node *prev;
struct Node *next;
} node;
typedef struct IteratorInt{
node *head;
node *tail;
node *last;
} IteratorInt;
IteratorInt IteratorIntNew(){
IteratorInt *listint;
listint = malloc(sizeof(IteratorInt));
listint->head = NULL;
listint->tail = NULL;
listint->last = NULL;
printf("address made %u\n", listint);
return *listint;
}
int add(IteratorInt *it, int v){
node *n;
n->val = v;
n->next = NULL;
n->prev = NULL;
printf("func works\n");
printf("n %d\n", n->val);
printf("address %u", it);
it->head = n;
printf("result %d", it->head->val);
if (it->head == NULL){
printf("worksfine");
}
/* if (it->head == 0){
it->head = n;
}
if (it->tail == 0){
it->tail = n;
}
if (it->last == 0){
it->last = n;
}*/
return 1;
}
int main() {
IteratorInt lit = IteratorIntNew();
printf("works %u", &lit);
add(&lit, 10);
/*printf("Node value %d\n", lit.head.val);
add(&lit, 15);
printf("Node value %d", lit.tail.val);*/
return 0;
}
あなたはそれで間違っているものを私に言うことはできますか?それを解決する方法は?どうもありがとう。
ほとんどの場合: 'it'がNULLで、そしてあなたが' nullで> head'に取得することはできません。問題のある行を見つけるのに十分なデバッグをお勧めします! – John3136
さて、あなたはそれを割り当てていません... –
@ John3136、if文を削除します。上記の 'it-> head'につながるprintf文は正常に動作します。 – garjted