使用malloc为结构的多维数组 [英] using malloc for multidimensional array of struct

查看:131
本文介绍了使用malloc为结构的多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个基本的问题,但我想分配内存的结构的3维数组。
我想从文件中读取双打并希望在结构来存储。第一行是块号(因为这将是永远1这里不相关),第二行表示的x中网格点的数量,Y和Z分别坐标。在这种情况下,10个点在X,5在Y和1在Z方向上。而从第三行是X,Y,这是我想读双打每个点Z坐标。首先有所有的X分量(即10 * 5 * 1×坐标,然后类似地Y和Z)。文件格式是这样的:

This is probably a basic question but I want to allocate the memory for 3 dimensional array of a struct. I'm trying to read doubles from a file and want to store in struct. The first line is block number (not relevant here as it'll be 1 always), second line denotes the number of grid points in X, Y and Z coordinate respectively. In this case 10 points in X, 5 in Y and 1 in Z direction. And from third line, are the X,Y,Z coordinates of each points which are the doubles I would like to read. First there are all X components (i.e. 10*5*1 x coordinates, then similarly Y and Z). The file format is like this:

      1
      10        5        1
  0.000000e+00   1.111111e+00   2.222222e+00   3.333333e+00 
  4.444445e+00   5.555555e+00   6.666667e+00   7.777778e+00 
  8.888889e+00   1.000000e+01   0.000000e+00   1.111111e+00 
  2.222222e+00   3.333333e+00   4.444445e+00   5.555555e+00 
  6.666667e+00   7.777778e+00   8.888889e+00   1.000000e+01 
  0.000000e+00   1.111111e+00   2.222222e+00   3.333333e+00 
  4.444445e+00   5.555555e+00   6.666667e+00   7.777778e+00 
  8.888889e+00   1.000000e+01   0.000000e+00   1.111111e+00 
  2.222222e+00   3.333333e+00   4.444445e+00   5.555555e+00 
  6.666667e+00   7.777778e+00   8.888889e+00   1.000000e+01 
  0.000000e+00   1.111111e+00   2.222222e+00   3.333333e+00 
  4.444445e+00   5.555555e+00   6.666667e+00   7.777778e+00 
  8.888889e+00   1.000000e+01...and so on...

我可以先读取4个整数,所以我知道我要存储的数据点的数量。然后我使用malloc函数分配内存和存储在变量中的数据。当我执行程序时,它读取整数,但无法读取双打。什么是我做错误?

I can read the first 4 integers and hence I know the number of points I wish to store data for. Then I'm using malloc function to allocate the memory and store the data in the variables. When I execute the program, it reads the integers but fails to read the doubles. What is the mistake I'm making?

下面是我的code:

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

typedef struct{
    double x,y,z;
}Mesh;

int main(void)
{
    int nblocks, IMAX, JMAX, KMAX;

    Mesh ***grid;

    FILE *mesh = fopen("test.x","r");

    fscanf(mesh,"%i %i %i %i",&nblocks,&IMAX,&JMAX,&KMAX);
    printf("%i %i %i %i\n",nblocks,IMAX,JMAX,KMAX);

    grid = malloc(sizeof(Mesh)*nblocks*IMAX*JMAX*KMAX);

    fscanf(mesh,"%lf",&grid[0][0][0].x);
    printf("%lf\n",grid[0][0][0].x);

    fclose(mesh);

    return 0;
}

在编译但开不读/写我存储在该结构的变量x变量的程序没有给出任何错误。 (如果一切正常,我可以把它放在循环,用于读取我没有在这里完成的所有值。)

The program doesn't give any error while compiling but it does't read/write the variable I stored in the x variable of the struct. (If this works, I can put it in loop for reading all values which I've not done here.)

如果我定义网​​格[IMAX] [JMAX] [KMAX] 后,我读 IMAX,JMAX,KMAX ,我得到正确的输出。但要知道这样的作品如何指针方式。

If I define Mesh grid[IMAX][JMAX][KMAX] after I read in IMAX,JMAX,KMAX, I get correct output. But wanted to know how pointer way of doing works.

感谢您,
Pranav

Thank you, Pranav

推荐答案

您有一个四维数组,而不是三个。的尺寸是:

You have a four dimensional array, not three. The dimensions are:


  1. 的nblocks

  2. IMAX

  3. JMAX

  4. KMAX

  1. nblocks
  2. IMAX
  3. JMAX
  4. KMAX

因此​​,对于电网的类型必须是网​​格**** ,而不是网格***

Hence, the type for grid has to be Mesh****, not Mesh***.

Mesh ****grid;

您code为电网分配内存必须是:

Your code to allocate memory for grid has to be:

grid = malloc(nblocks * (sizeof *grid));
for ( block = 0; block < nblocks; ++block )
{
   grid[block] = malloc(IMAX * (sizeof *grid[0]));
   for ( i = 0; i < IMAX ; ++i )
   {
      grid[block][i] = malloc(JMAX * (sizeof *grid[0][0]));
      for ( j = 0; j < JMAX ; ++j )
      {
         grid[block][i][j] = malloc(KMAX * (sizeof *grid[0][0][0]));
      }
   }
}

现在,您可以使用访问网格数据:

Now, you can access the grid data using:

grid[block][index][jindex][kindex]

这是有效的用法:

These are valid usages:

fscanf(mesh,"%lf",&grid[0][0][0][0].x);
printf("%lf\n",grid[0][0][0][0].x);

这篇关于使用malloc为结构的多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆