简体   繁体   English

结构内部指向动态二维数组的指针

[英]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. 我正在使用线程,我希望它们都修改相同的2D数组。 So I made a struct with a pointer to the 2D array, but it isn't working. 所以我用指向2D数组的指针制作了一个结构,但是它不起作用。 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). 如您所见,参数具有变量“ celdaMatriz matriz **”(对我的代码中的西班牙语感到抱歉)。 Now comes a function to initialize a "celdaMatriz" 2D array 现在提供了一个用于初始化“ celdaMatriz” 2D数组的函数

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. 函数createMatriz将改变,但这就是想法。 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. 因此,无论何时我想创建一个新线程,我都会创建一个新的参数变量,其“ matriz”变量是指向我在主函数中创建的2D数组的指针。 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. 错误来自createMatrix函数,最后一个来自main函数。 By the way I'm using linux and C. Thanks. 顺便说一下,我正在使用linux和C。谢谢。 Hope you can help me. 希望您能够帮助我。

EDITED 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. 我刚刚修复了一个错误,该* 从不兼容的指针类型传递了'crearMatriz'的参数3 [默认启用] main.c:59:6:注意:预期的'struct celdaMatriz *)[(unsigned int)(totaly)] ',但参数的类型为'struct celdaMatriz *已经固定。 So the problem now is that I'm accessing matriz[i][j]->XY=' '; 所以现在的问题是我正在访问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. 我正在使用线程,我希望它们都修改相同的2D数组。 So I made a struct with a pointer to the 2D array, but it isn't working. 所以我用指向2D数组的指针制作了一个结构,但是它不起作用。 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). 如您所见,参数具有变量“ celdaMatriz matriz **”(对我的代码中的西班牙语感到抱歉)。 Now comes a function to initialize a "celdaMatriz" 2D array 现在提供了一个用于初始化“ celdaMatriz” 2D数组的函数

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. 函数createMatriz将改变,但这就是想法。 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. 因此,无论何时我想创建一个新线程,我都会创建一个新的参数变量,其“ matriz”变量是指向我在主函数中创建的2D数组的指针。 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. 错误来自createMatrix函数,最后一个来自main函数。 By the way I'm using linux and C. Thanks. 顺便说一下,我正在使用linux和C。谢谢。 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. 我刚刚修复了一个错误,该* 从不兼容的指针类型传递了'crearMatriz'的参数3 [默认启用] main.c:59:6:注意:预期的'struct celdaMatriz *)[(unsigned int)(totaly)] ',但参数的类型为'struct celdaMatriz *已经固定。 So the problem now is that I'm accessing matriz[i][j]->XY=' '; 所以现在的问题是我正在访问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 因此,解决方案是1-声明全局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 createMatriz函数:

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, 我想问题是您试图这样叫creatMatriz

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

and C (C99) does not like it. C(C99)不喜欢它。 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] . 问题是p1->matriz的类型为struct celdaMatriz ** ,但是您的createMatriz原型要求struct * (*) [totalY]因为C忽略了[totalX]

If you want to keep the definition of parameter unchanged, I would suggest you just use a double pointer. 如果要保持parameter的定义不变,建议您使用双指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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