如何分配一个2D数组的指针到struct[复制]

时间:2021-04-03 21:36:11

This question already has an answer here:

这个问题已经有了答案:

This is were I got so far,but I don't know if it's right. This function receives the dimensions of the 2D array (nxn),and allocates it. flightInfo is the name of the struct. Will this work? thanks in advanced

这是我到目前为止所得到的,但我不知道它是否正确。该函数接收二维数组(nxn)的维数,并分配它。flightInfo是结构的名称。这工作吗?由于先进的

after allocating the array(ignore the method ,since we are not allowed to use the method you proposed) I would like to initialize the struct (I built a function to do it but it didn't work),I tried to do it right after the allocation and kept getting the" Unhandled exception" warning, does it has to do with the syntax, am I forgetting a '*'?

后分配数组(忽略方法,因为我们不允许使用方法提出了)我想初始化结构体(我做了一个函数来做,但它没有工作),我试图做正确的分配和后得到“未处理的例外”警告,这与语法,我忘记‘*’吗?

void flightMatrix()
{
 FILE * fpf;
 int checkScan,Origin,Dest;
 float  time,cost;
 char flightName[3];
 flightInfo *** matrix;


 if(!(fpf=fopen("flights.txt","r")))exit(1);

  while((checkScan=fscanf(fpf,"%*10c%3d%3d%3c%5f%7f%*",&Origin,&Dest,flightName,&time,&cost))!=EOF)
{
    matrix=allocateMatrix(Dest);
   matrix[Origin-1][Dest-1]->o=Origin;

}

}

}

flightInfo*** allocateMatrix(int n)

{   int i,j;
    flightInfo***  matrix;

matrix=(flightInfo***)malloc(sizeof(flightInfo **)*n);
    for(i=0;i<n;i++)
matrix[i]=(flightInfo **)malloc(sizeof(flightInfo*)*n);

 for (int i = 0; i < n; ++i) 
 {  
    for (int j = 0; j < n; ++j) 
        matrix[i][j] = NULL;
 }

    return matrix;
}

[http://i.stack.imgur.com/MFC7V.png] this is what happens when I try to initialize

这就是我尝试初始化时所发生的事情。

1 个解决方案

#1


0  

Technically speaking, this won't create 2D array. The result will be array of pointers, where each one points to different array of pointers to a struct.

从技术上讲,这不会创建2D数组。结果将是指针数组,其中每个指针指向一个结构的不同指针数组。

The difference is that, memory will be fragmented, so every element will point to some memory location, instead of single continuous memory block.

不同之处在于,内存将被分割,因此每个元素都指向某个内存位置,而不是单个连续内存块。

The common approach for this is to create flatten 2D array:

通常的方法是创建flatten 2D数组:

flightInfo** allocateMatrix(int n)
{
    flightInfo** matrix = malloc(n*n * sizeof(*matrix));
    for (int i = 0; i < n; i++) 
        for (int j = 0; j < n; j++) 
            matrix[i*n + j] = NULL;
    return matrix;
}

If you are forced to use two indices, then you could place matrix as function argument:

如果你*使用两个指标,那么你可以将矩阵作为函数参数:

void allocateMatrix(int n, flightInfo* (**matrix)[n])
{
    *matrix = malloc(n * sizeof(**matrix));
    for (int i = 0; i < n; ++i) 
        for (int j = 0; j < n; ++j) 
            (*matrix)[i][j] = NULL;
}

The second asterisk is required, because pointers are passed by value, otherwise you would end up with modified local copy of the pointer, that does nothing to matrix from main function.

第二个星号是必需的,因为指针是按值传递的,否则就会得到指针的修改本地副本,这对主函数没有任何作用。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct flightInfo {
    char airport[30];
    int  altitude;
} flightInfo;

void allocateMatrix(int n, flightInfo* (**matrix)[n])
{
    *matrix = malloc(n * sizeof(**matrix));
    for (int i = 0; i < n; ++i) 
        for (int j = 0; j < n; ++j) 
            (*matrix)[i][j] = NULL;
}

int main()
{
    int n = 10;
    flightInfo* (*matrix)[n];

    allocateMatrix(n, &matrix);

    matrix[0][0] = malloc(sizeof(flightInfo));
    strcpy(matrix[0][0]->airport, "Heathrow");
    matrix[0][0]->altitude = 10000;

    printf("%s, %d\n", matrix[0][0]->airport, matrix[0][0]->altitude);
}

The another way would be to encapsulate the array within a struct.

另一种方法是将数组封装到结构中。

#1


0  

Technically speaking, this won't create 2D array. The result will be array of pointers, where each one points to different array of pointers to a struct.

从技术上讲,这不会创建2D数组。结果将是指针数组,其中每个指针指向一个结构的不同指针数组。

The difference is that, memory will be fragmented, so every element will point to some memory location, instead of single continuous memory block.

不同之处在于,内存将被分割,因此每个元素都指向某个内存位置,而不是单个连续内存块。

The common approach for this is to create flatten 2D array:

通常的方法是创建flatten 2D数组:

flightInfo** allocateMatrix(int n)
{
    flightInfo** matrix = malloc(n*n * sizeof(*matrix));
    for (int i = 0; i < n; i++) 
        for (int j = 0; j < n; j++) 
            matrix[i*n + j] = NULL;
    return matrix;
}

If you are forced to use two indices, then you could place matrix as function argument:

如果你*使用两个指标,那么你可以将矩阵作为函数参数:

void allocateMatrix(int n, flightInfo* (**matrix)[n])
{
    *matrix = malloc(n * sizeof(**matrix));
    for (int i = 0; i < n; ++i) 
        for (int j = 0; j < n; ++j) 
            (*matrix)[i][j] = NULL;
}

The second asterisk is required, because pointers are passed by value, otherwise you would end up with modified local copy of the pointer, that does nothing to matrix from main function.

第二个星号是必需的,因为指针是按值传递的,否则就会得到指针的修改本地副本,这对主函数没有任何作用。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct flightInfo {
    char airport[30];
    int  altitude;
} flightInfo;

void allocateMatrix(int n, flightInfo* (**matrix)[n])
{
    *matrix = malloc(n * sizeof(**matrix));
    for (int i = 0; i < n; ++i) 
        for (int j = 0; j < n; ++j) 
            (*matrix)[i][j] = NULL;
}

int main()
{
    int n = 10;
    flightInfo* (*matrix)[n];

    allocateMatrix(n, &matrix);

    matrix[0][0] = malloc(sizeof(flightInfo));
    strcpy(matrix[0][0]->airport, "Heathrow");
    matrix[0][0]->altitude = 10000;

    printf("%s, %d\n", matrix[0][0]->airport, matrix[0][0]->altitude);
}

The another way would be to encapsulate the array within a struct.

另一种方法是将数组封装到结构中。