C - Getting a struct from a function by pointer - segmentation fault -


i'm new c, , i'm having great deal of trouble 1 function. have struct declared as:

struct nivel {     size_t filas;     size_t columnas;     int **mapa; }; 

it's 2d array, it's size info. now, have function reads text file , makes "nivel", it's defined as:

void nuevo_nivel_desde_archivo(struct nivel * nuevo_nivel, char *nombre_archivo ){  nuevo_nivel->filas = 0;  nuevo_nivel->columnas = 0;  ... 

i'll post part, because it's problem resides. thought make function receives pointer structure , "fill it", have call function this:

struct nivel *nuevo_nivel; nuevo_nivel_desde_archivo(nuevo_nivel,nombre_archivo); 

nombre_archivo holds name of text file. when try assign cero of fields of struct, segmentation fault error. pointers, should work, i'm afraid i'm missing here , making huge mistake. appreciated.

edit: everyone! of stated, trying access memory wasn't allocated, null pointer. have problem, that'll post. thanks!

the problem nuevo_nivel uninitialized. need allocate either stack or heap first, so:

// stack struct nivel nuevo_nivel; nuevo_nivel_desde_archivo(&nuevo_nivel,nombre_archivo);  // heap struct nivel *nuevo_nivel = malloc(sizeof(nivel)); nuevo_nivel_desde_archivo(nuevo_nivel,nombre_archivo); 

which kind of memory allocate depends on scoping/lifetime requirements nuevo_nivel.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -