【发布时间】:2016-04-21 04:03:37
【问题描述】:
第一次在这里提问,如果我错过了什么,很抱歉。 我正在尝试用 C 对物理学中的 2D 静电问题进行建模。 我有一个主数组,我将在其中存储每个点的电势和电荷密度值,然后是一个指针数组,它将内存地址提供给主数组。
但是我不知道编译时的数组大小,因为它是运行时的用户输入,因此需要能够动态分配内存。 这是我已经拥有的,任何帮助表示赞赏。谢谢!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
typedef struct tTuple { //Create new type called tuple
double poten; //potential
double cden; //charge density
} tuple;
int i, j;
int N, M;
#define a 10 //Grid Width
#define b 10 //Grid Height
int x1, y1, x2, y2; //Positions of two point charges
int my_rank, comm_size;
double w;
tuple mainarray [a][b];
double *pointerarray[a][b];
int convflag = 1; //Global convergence checker flag
/* More code below with main function containing scanf etc */
【问题讨论】:
标签: c arrays dynamic malloc 2d