简体   繁体   中英

pointer inside struct to dynamic 2-D array

Hope you can help me since I'm a little desperate right now. Im working with threads and I want them all to modify the same 2D array. So I made a struct with a pointer to the 2D array, but it isn't working. This is my code

typedef struct {
    char XY;
    int b;
    int d;
    int f;
    int h;

} celdaMatriz;

typedef struct {
    int totalX;
    int totalY;
    celdaMatriz **matriz;
    char direction;
    int i;
    int j;
    int cont;

} parameter;

As you can see parameter has the variable " celdaMatriz matriz**" (I'm sorry about the spanish in my code). Now comes a function to initialize a "celdaMatriz" 2D array

void createMatriz(int totalX, int totalY, celdaMatriz **matriz[totalX][totalY])
{
    int i,j;
    for(i=0;i<totalX; i++){
        for(j=0; j<totalX;j++){
            matriz[i][j]->XY=' ';
            matriz[i][j]->b=0;
            matriz[i][j]->d=0;
            matriz[i][j]->f=0;
            matriz[i][j]->h=0;
        }
    }

    matriz[0][3]->XY='/';
    matriz[2][0]->XY='*';
    matriz[2][1]->XY='*';
    matriz[0][2]->XY='*';
    matriz[2][2]->XY='*';
    matriz[1][3]->XY='*';
    matriz[3][3]->XY='*';
}

the function createMatriz will change, but that's the idea. So now my main function

int main( int argc,char *argv[])
{
    int i,j;
    celdaMatriz **matriz = malloc( sizeof (*matriz) * 4);
    for (i=0; i < 4; ++i)
        matriz[i] = malloc(sizeof (**matriz) * 4);

     crateMatriz(4,4,matriz); 
    parameter *p1,p2;

    parameter *p1 = malloc(sizeof(*p1));
    p1->totalX=4;
    p1->totalY=4;
    p1->i=0;
    p1->j=0;
    p1->cont=0;
    p1->direction= 'f';
    p1->matriz= matriz;
    return 0;
}

So anytime I want to create a new thread I create a new parameter variable and its "matriz" variable is a pointer to the 2D array I created in the main function. Well at least that's the idea, but I get these errors

 **request for member 'XY' in something not a structure or union**
 **request for member 'b' in something not a structure or union**
 **request for member 'd' in something not a structure or union**
 **request for member 'f' in something not a structure or union**
 **request for member 'h' in something not a structure or union**
  passing argument 3 of 'crearMatriz' from incompatible pointer type [enabled by        default]
    main.c: 59:6: note: expected 'struct celdaMatriz ** (*) [(unsigned int) (totaly)]'         but argument is of type 'struct celdaMatriz **

The errors come from the createMatrix function and the last one from the main function. By the way I'm using linux and C. Thanks. Hope you can help me.

EDITED

I just fixed an error, this * passing argument 3 of 'crearMatriz' from incompatible pointer type [enabled by default] main.c: 59:6: note: expected 'struct celdaMatriz * ( ) [(unsigned int) (totaly)]' but argument is of type 'struct celdaMatriz * is already fixed. So the problem now is that I'm accessing matriz[i][j]->XY=' '; incorrectly Hope you can help me since I'm a little desperate right now. Im working with threads and I want them all to modify the same 2D array. So I made a struct with a pointer to the 2D array, but it isn't working. This is my code

typedef struct {
    char XY;
    int b;
    int d;
    int f;
    int h;

} celdaMatriz;

typedef struct {
    int totalX;
    int totalY;
    celdaMatriz **matriz;
    char direction;
    int i;
    int j;
    int cont;

} parameter;

As you can see parameter has the variable " celdaMatriz matriz**" (I'm sorry about the spanish in my code). Now comes a function to initialize a "celdaMatriz" 2D array

void createMatriz(int totalX, int totalY, celdaMatriz **matriz[totalX][totalY])
{
    int i,j;
    for(i=0;i<totalX; i++){
        for(j=0; j<totalX;j++){
            matriz[i][j]->XY=' ';
            matriz[i][j]->b=0;
            matriz[i][j]->d=0;
            matriz[i][j]->f=0;
            matriz[i][j]->h=0;
        }
    }

    matriz[0][3]->XY='/';
    matriz[2][0]->XY='*';
    matriz[2][1]->XY='*';
    matriz[0][2]->XY='*';
    matriz[2][2]->XY='*';
    matriz[1][3]->XY='*';
    matriz[3][3]->XY='*';
}

the function createMatriz will change, but that's the idea. So now my main function

int main( int argc,char *argv[])
{
    int i,j;
    celdaMatriz **matriz = malloc( sizeof (*matriz) * 4);
    for (i=0; i < 4; ++i)
        matriz[i] = malloc(sizeof (**matriz) * 4);

     crateMatriz(4,4,matriz); 
    parameter *p1,p2;

    parameter *p1 = malloc(sizeof(*p1));
    p1->totalX=4;
    p1->totalY=4;
    p1->i=0;
    p1->j=0;
    p1->cont=0;
    p1->direction= 'f';
    p1->matriz= matriz;
    return 0;
}

So anytime I want to create a new thread I create a new parameter variable and its "matriz" variable is a pointer to the 2D array I created in the main function. Well at least that's the idea, but I get these errors

 **request for member 'XY' in something not a structure or union**
 **request for member 'b' in something not a structure or union**
 **request for member 'd' in something not a structure or union**
 **request for member 'f' in something not a structure or union**
 **request for member 'h' in something not a structure or union**
  passing argument 3 of 'crearMatriz' from incompatible pointer type [enabled by        default]
    main.c: 59:6: note: expected 'struct celdaMatriz ** (*) [(unsigned int) (totaly)]'         but argument is of type 'struct celdaMatriz **

The errors come from the createMatrix function and the last one from the main function. By the way I'm using linux and C. Thanks. Hope you can help me.

EDIT

I just fixed an error, this * passing argument 3 of 'crearMatriz' from incompatible pointer type [enabled by default] main.c: 59:6: note: expected 'struct celdaMatriz * ( ) [(unsigned int) (totaly)]' but argument is of type 'struct celdaMatriz * is already fixed. So the problem now is that I'm accessing matriz[i][j]->XY=' '; incorrectly

EDIT Hi. I just want to let you know that I already found an answer to my problem. Instead of send a pointer to the matrix, I will use it as a global variable, that way all the threads will be able to modify it. So the solution is 1- Declare a global celdaMatriz **matriz

celdaMatriz **matriz;

Next I changed the memory allocation

matriz = (celdaMatriz **) malloc(4 * sizeof(celdaMatriz *));

for (i = 0; i < 4; ++i)
{
    matriz[i] = (celdaMatriz *) malloc(4 * sizeof(celdaMatriz));
}

Finally the new void createMatriz function:

void crearMatriz(int totalX, int totalY)
{
    int i=0,j=0;
    for(i=0; i<totalX;i++){
        for(j=0; j<totalY; j++){
            celdaMatriz *nodo = malloc(sizeof(celdaMatriz));
            nodo->b=0;
            nodo->d=0;
            nodo->f=0;
            nodo->h=0; 
            nodo->casilla=0;
            matriz[i+j*totalY]=(celdaMatriz *)nodo;

        }
    }

    matriz[12]->casilla=1; 
    for(i=0; i<totalX; i++){
        for(j=0; j<totalY; j++){
            printf(" %d ", matriz[i+j*totalY]->casilla); 
        }
        printf("\n"); 
     }
}

And that's it. Thanks to all of you who helped me with this problem

I guess that the problem is that you tried to call creatMatriz like this,

      createMatriz(p1->totalX, p1->totalY, p1->matriz);

and C (C99) does not like it. The problem is that p1->matriz is of type struct celdaMatriz ** , but your prototype of createMatriz asked for a struct * (*) [totalY] because C ignores [totalX] .

If you want to keep the definition of parameter unchanged, I would suggest you just use a double pointer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM