Change contents in a given position in a multidimensional array within a structure

Asked

Viewed 51 times

1

I’m having trouble modifying the content in a given position of a multidimensional array that is within a structure. I have two structures struct TabelaFilial and struct ArrTabela. The structure struct TabelaFilial is being pointed out by an incomplete guy typedef struct TabelaFilial *TabelaFiliaisand the structure struct ArrTabela for typedef struct ArrTabela *ArrTabelaFiliais . The structure TabelaFilial has a matrix with 14 lines : char *(*m)[14] and the structure ArrTabela has an array with 3 positions in which each position points to the structure TabelaFilial. Here are the structures :

struct TabelaFilial

{
 char *(*m)[14];
};

typedef struct TabelaFilial *TabelaFiliais;

struct ArrTabela
{
 TabelaFiliais tab[3];

};

typedef struct ArrTabela *ArrTabelaFiliais;

So I initialized the structures:

void iniciaTabela (TabelaFiliais t)
{
    t = (TabelaFiliais ) malloc (sizeof (struct TabelaFilial));
    t->m = malloc (sizeof (char*) * 9); 

    int l , c;

    for (l = 0 ; l < 14 ; ++l)        
     for (c = 0 ; c < 9 ; ++c)      
        t->m[l][c] = NULL;

}

void iniciaArr (ArrTabelaFiliais arr)
{
    int i;
    arr = (ArrTabelaFiliais)malloc(sizeof (struct ArrTabela));

    for (i = 0 ; i < 3 ; ++i)
      iniciaTabela(arr->tab[i]);


}

Intended to access the array tab[3] at position 0 of the structure ArrTabela and change its content. I used this little function:

void TabelaFilial (ArrTabelaFiliais arr)
{
    int l , c;


    for (l = 0 ; l < 14 ; ++l)        
     for (c = 0 ; c < 9 ; ++c)      
       strcpy ((arr->tab[0]->m[l][c]) , "||||||");
}  

However, I did get a Segmentation fault (core dumped). What am I doing wrong? And how can I correct?

  • the field of tabelaFinal is char *(*m)[14], when the variable is between parentheses, it means it will store methods, try to do without using parentheses, char *m[14]. And make sure you’re allocating the memory of the elements correctly.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.