0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
    char** thingSize;
} Thing;

typedef struct {
    Thing* thing;
} Game;

void load_array(Thing* thing) {
    int i, j;
    char **emptyThing = malloc(sizeof(char**));
    emptyThing = malloc(sizeof(char*)*9);
    for(i=0; i<9; i++) {
        emptyThing[i] = malloc(sizeof(char)*9);
    }
    thing -> thingSize = emptyThing;
    free(emptyThing);
}

int main(int argc, char* argv[]) {
    Game* game;
    load_array(game -> thing);
    printf("HI");
}

I am getting a segmentation fault, I have found that the problem line is. thing -> thingSize = emptyThing; I am trying to set thingSize to be a 2d array equal to emptyThing.

CaptainGir
  • 15
  • 4
  • 1
    Where do you get the magic number `9` from? – AKX Aug 28 '20 at 06:50
  • 5
    `game` is not initialized... – fredrik Aug 28 '20 at 06:51
  • Your whole code looks pretty pointless. I think you should study pointers and try to write really simple programs involving pointers before trying to write code that involve multiple pointers, pointer to pointers, arrays of pointers etc. – Jabberwocky Aug 28 '20 at 07:08

1 Answers1

2

As Fredrik said, the game pointer is not initialized to anything. It hold a garbage value, when dereferencing it, you will get a segfault.

srv236
  • 509
  • 3
  • 5