0

How can I check if recursive children of the struct defined in my code are NULL (or empty, not used)? (I want to know if they are NULL so that I can fill them with data).

#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;
}

At this line fs->child[i] == NULL and this line fs->child[i] = e it returns Segmentation fault: 11 during Runtime. What am I doing wrong?

L. Carbini
  • 55
  • 1
  • 5

1 Answers1

1

FS *fs; should be instead FS *fs = (FS*) malloc (sizeof(FS));. Most likely you know how to allocate a memory block but you seem as forgot it. However, don't forget to make it free(fs);