函数中的多维动态结构化数组访问 [英] Multidimensional Dynamic Structured Array Acces among Functions
问题描述
亲爱的大家,
我是这个论坛的新手,我是C编程的新手,所以请原谅我将要犯的任何基本错误。
我正在尝试为程序中所有函数可用的全局结构化指针数组动态分配空间。由于C在函数调用中不允许打开数组参数,并且我不知道编译时数组的大小,我不知道如何将此数组传递给子函数。此外,由于我不确定内存分配是否是连续的,只是传递数组的起始地址对我来说是无用的。
例如我生成以下arrray:
====================== ===================
typedef struct {
float Rad; / *计算半径* /
浮动Az; / *计算方位角* /
浮动El; / *计算海拔* /
int Incr; / *体素平均值计数器* /
浮点数Dm; / *网格化数据掩码* /
} GRID_STRUCT;
/ * 3D笛卡尔结构化阵列的分配内存* /
GRID_STRUCT(* grid)[(x_size)] [(y_size)] [z_size] = malloc(sizeof(* grid));
if(grid == NULL)
{
printf(" \ nnError,内存未分配给>> grid<<。\ nn");
退出(1);
}
====================== ============================ ==
在其封闭函数中使用此数组没关系
只需输入以下内容即可访问并轻松编写:
&((* grid)(x_position)(y_position)( z_position).Rad)= whatever_address或
无论=(正确演员)(*网格)(x_position)(y_position)(z_position).Rad;
现在,我的问题是
如何将这个结构化数组传递给其他函数,以便他们可以以相同的方式访问它。
如果我只能使用它会很好:
extern struct GRID_STRUCT(* grid)[x_size] [y_size] [z_size] ;
int temp_Rad =(* grid)[x_smthng] [y_smthng] [z_smthng] .flag;
访问结构,但它不是工作,因为编译器给我一个错误
任何建议将不胜感激。
谢谢
Edo
Dear everyone,
I am new to this forum and I am realitevely new to C programming so please forgive me for any basic mistakes I''ll be making.
I am trying to dynamically allocate the space for the global structured array of pointers that is available to all functions in my program. Since C doesn''t allow open array arguments in funciton calls and I do not know the size of the array at compile time I am not sure how to pass this array to the sub- functions. Also, since I am not sure whether the memory allocation will be continuous, just passing the origination address of the array is useless to me.
For example I generate the following arrray:
=========================================
typedef struct{
float Rad; /*to calculate radius */
float Az; /*to calculate azimuth */
float El; /*to calculate elevation */
int Incr; /* Counter for voxel averaging */
float Dm; /* Gridded datamask */
}GRID_STRUCT;
/* Allocationg memory fro 3D Cartesian structured array */
GRID_STRUCT(*grid)[(x_size)][(y_size)][z_size]=malloc(sizeof(*grid));
if( grid == NULL)
{
printf("\nError, memory not allocated for >> grid <<.\n");
exit(1);
}
================================================== ==
Working with this array within its enclosing function is OK.
I can access it and write it easiliy by simply typing:
&( (*grid)(x_position)(y_position) (z_position).Rad) = whatever_address or
whatever =(proper cast) (*grid)(x_position)(y_position) (z_position).Rad;
Now, my question is
how can I pass this structured array to other functions so they can access it in simmilar manner.
It would be nice if I could only use:
extern struct GRID_STRUCT (*grid) [x_size][y_size][z_size];
int temp_Rad=(*grid)[x_smthng][y_smthng][z_smthng].flag;
to access the structure but it doesn''t work since compiler gives my an errror
Any advice will be much appreciated.
Thanks
Edo
推荐答案
帮助任何人?
帮助任何人?
Help Anyone ?
你需要全局访问它。
注意:要全局访问它,请使用::
Savage
You need to access it globaly.
Note:To access it globally use ::
Savage
不幸的是我使用的是C(不是C ++),所以::不会工作
Unfortunately I am using C (not C++),so :: will not work
这篇关于函数中的多维动态结构化数组访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!