私のコードで定義された構造体の再帰子がNULL(または空で未使用)であるかどうかを確認するにはどうすればよいですか? (私は彼らがデータでそれらを埋めることができるようにNULLであるかどうかを知りたい)。このラインfs->child[i] == NULL
と、このラインでstructの再帰子がNULL(C言語)
#include <stdio.h>
#include <stdlib.h>
#define HEIGHT 256
#define LENGTH 256
typedef struct FS FS;
typedef struct Elem Elem;
struct Elem {
char name[256];
char content[256];
Elem *child[1024];
};
struct FS {
Elem *child[1024];
};
void create(FS *fs, char path[HEIGHT][LENGTH]){
while(i<1024){
if(fs->child[i] == NULL){ //check if child[i] is NULL, if so I can fill it with data
Elem *e;
e = malloc(sizeof (Elem));
fs->child[i] = e;
strcpy(e->name, path[0]);
i = 1024;
}
i++;
}
}
int main(void) {
FS *fs;
char path[HEIGHT][LENGTH];
create(fs, path);
return 0;
}
それは実行時にSegmentation fault: 11
を返しfs->child[i] = e
。私は間違って何をしていますか?
あなたは 'fs'に有効なポインタ値を割り当てませんでした。したがって、 'fs - > ...'は不正なアドレス参照です。 – lurker
'fs'はどこで作成しますか? –
まず、 'malloc'のどこかで' fs'に有効なポインタを割り当て、 'memset'ですべてをゼロに初期化する必要があります。 –