I've this struct:
struct _window{
int bcolor; /* background color */
int fcolor; /* foreground color */
int x; /* x coordinate */
int y; /* y coordinate */
int sx; /* lenght */
int sy; /* high*/
char **matrix; /* Matrix with chars */
};
I use this function to initialize the window
window* ini_window(int bcolor, int fcolor, int x, int y, int sx, int sy) {
window *v;
int i;
v = (window*) malloc(sizeof (window));
if (!v) /* CDE */
return NULL;
v->bcolor = bcolor;
v->fcolor = fcolor;
v->x = x;
v->y = y;
v->sx = sx;
v->sy = sy;
/* allocate the matrix */
v->matrix = (char**) calloc(sy, sizeof (char*));
if (!v->matrix) { /* CDE */
free(v);
return NULL;
}
/* allocate the rows */
for (i = 0; i < sy; i++) {
v->matrix[i] = (char*) calloc(sx + 1, sizeof (char));
if (!v->matrix[i]) { /* CDE */
free(v->matrix);
free(v);
return NULL;
}
v->matrix[i] = "\0"; /*<- delete this line to solve */
}
return v;
}
I try to load the matrix with that function
int cargar_matriz_file(window*v, char *nom_file) {
int i;
char aux[100];
FILE *pf;
if (!v)
return -1;
if (!v->matrix)
return -1;
pf = fopen(nom_file, "r");
if (!pf) /* CDE */
return -1;
for (i = 0; i < v->sy; i++) {
fgets(aux, v->sx, pf);
if (v->matrix[i]) {
strcpy(v->matrix[i], aux); /* <-segmentation fault here */
strtok(v->matrix[i], "\r\n");
}
}
fclose(pf);
return 0;
}
But there is an error in the line with the strcpy, aux have the correctly lenght and v->matrix[i] is allocated, what could be happening?