我的代码非常混乱,有两个结构,它们的定义和初始化都相同。但是对于tall
结构,我可以将变量存储在结构tall[radius]
中而没有任何问题。但是,当我复制xx
结构的过程时,doenst工作会打印错误的值。
我似乎无法弄清楚结构用法的问题。
我需要一个结构体数组来每次动态存储一个大小不同的数组,并且稍后每个结构体都需要其他元素。
我也乐于接受新方法。 为什么两个结构的表现不同?
#include <stdio.h>
#include <stdlib.h>
#define ar_length 18 //this is the radius max
#define WIDTH 10 //xx width
struct ttall
{
int *pnt;
};
struct xall
{
int *pnt;
};
int main()
{
int i;
int m;
int *x1;
int *x2;
int *x3;
int *ttThis;
int rad_size;
//beginning of the loop
int radius = 1;
int size = (2 * radius + 1);
int size_sqrd = size * size * size;
struct ttall **tall = malloc(sizeof(struct ttall *));
struct ttall *struct_pnt = malloc( 3*sizeof(struct ttall));
struct xall **xx = malloc(sizeof(struct xall *));
struct xall *xpnt = malloc( 3*sizeof(struct xall));
for (int radius = 0; radius < 3; radius++)
{
//need to increment the pointer to the struct everytime
xx[radius] = xpnt+radius;
tall[radius] = struct_pnt+radius;
int z = -1;
int t = -radius-2;
printf("****T*** %d\n",t);
int rad;
int meshCount;
meshCount = (2 * (radius+1) + 1);
rad_size = (2 * (radius+1) + 1) * (2 * (radius+1) + 1) * (2 * (radius+1) + 1);
printf("radius size : %d\n",rad_size);
printf("mesh size : %d\n",meshCount);
tall[radius]->pnt = malloc(rad_size * sizeof(int));
xx[radius]->pnt = malloc(rad_size * WIDTH * sizeof(int));
x1 = malloc(rad_size * sizeof(int));
x2 = malloc(rad_size * sizeof(int));
x3 = malloc(rad_size * sizeof(int));
for (i = 0; i < meshCount; i++)
{
t++;
z++;
for (m = 0; m < meshCount*meshCount; m++)
{
x1[z * (meshCount*meshCount) + m] = t;
}
}
//x2 computations
i = 0;
m = 0;
z = 0;
t = -radius-2;
int x;
for (x = 0; x < meshCount; x++)
{
t++;
for (m = 0; m < meshCount; m++)
{
for(int h = 0;h<meshCount;h++){
int index = meshCount*x+(meshCount*meshCount)*h +m ;
x2[index] = t;
}
}
}
//x3 computations
i = 0;
m = 0;
z = 0;
t = -radius-2;
for (x = 0; x < meshCount; x++)
{
// t++;
t = -radius-2;
for (m = 0; m < meshCount; m++)
{
t++;
for(int h = 0;h<meshCount;h++){
int index = meshCount*x+(meshCount*meshCount)*h +m ;
x3[index] = t;
}
}
}
// structure initializations and memalocation
//works fine with expanding radius
for (m = 0; m < rad_size; m++)
{
tall[radius]->pnt[m] = (x1[m] * x1[m]) + (x2[m] * x2[m]) + (x3[m] * x3[m]);
}
// doesnt work here
m = 0;
for (i = 0; i < rad_size; i++)
{
xx[radius]->pnt[i * WIDTH ] = 1;
xx[radius]->pnt[i * WIDTH + 1] = x1[i];
xx[radius]->pnt[i * WIDTH + 2] = x2[i];
xx[radius]->pnt[i * WIDTH + 3] = x3[i];
xx[radius]->pnt[i * WIDTH + 4] = x1[i] * x1[i];
xx[radius]->pnt[i * WIDTH + 5] = x1[i] * x2[i];
xx[radius]->pnt[i * WIDTH + 6] = x1[i] * x3[i];
xx[radius]->pnt[i * WIDTH + 7] = x2[i] * x2[i];
xx[radius]->pnt[i * WIDTH + 8] = x2[i] * x3[i];
xx[radius]->pnt[i * WIDTH + 9] = x3[i] * x3[i];
}
}
//free(x1);
//free(x2);
//free(x3);
//***testing sum***
// the sum when radius = 1 of xx should be 171
int k = 0;
int sum = 0;
//can replace 27 with rad_size
for (k = 0; k < 27*WIDTH; k++)
{
sum = sum + abs(xx[0]->pnt[k]);
//printf("%d\n",abs(xx[0]->pnt[k]));
}
printf(" sum xx : %d\n", sum);
//******Testing****
for (int c = 0; c < 27; c++)
{
//printf("X2 : %d\n",x3[c]);
printf("tall : %d\n", tall[1]->pnt[c]);
//printf("%d\n",xx[c]);
}
//free(ttThis);
//free(xx);
}
答案 0 :(得分:0)
使用struct ttall **tall = malloc(sizeof(struct ttall *));
,您可以为一个指针分配空间。
但是稍后您用tall[radius]
对其进行索引,但是半径大于1。
您必须分配更多的内存:
struct ttall **tall = malloc(3 * sizeof(struct ttall *));`