浏览结构数组并存储值不正常的值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了浏览结构数组并存储值不正常的值相关的知识,希望对你有一定的参考价值。

我有这个非常混乱的代码,有两个结构,定义和初始化相同。但是对于tall结构,我可以在struct tall[radius]中存储变量而没有任何问题。但是,当我复制xx结构的过程时,doenst工作打印错误的值。我似乎无法弄清楚struct使用有什么问题。

我需要一个结构数组来每次动态地存储一个不同大小的数组,以及每个结构稍后的其他元素。

我也对新方法持开放态度。为什么两个结构的表现不同?

#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
",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
",rad_size);
        printf("mesh size : %d
",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
",abs(xx[0]->pnt[k]));
    }
    printf(" sum xx : %d
", sum);
    //******Testing****


    for (int c = 0; c < 27; c++)
    {
        //printf("X2 : %d
",x3[c]);
        printf("tall : %d
", tall[1]->pnt[c]);
        //printf("%d
",xx[c]);
    }


    //free(ttThis);
    //free(xx);
}
答案

使用struct ttall **tall = malloc(sizeof(struct ttall *));,您可以为一个指针分配空间。

但是后来你用tall[radius]索引它,但是半径大于1。

您必须分配更多内存:

struct ttall **tall = malloc(3 * sizeof(struct ttall *));`

以上是关于浏览结构数组并存储值不正常的值的主要内容,如果未能解决你的问题,请参考以下文章

Django 表单:无法显示验证错误

bash中的关联数组不在循环内存储值[重复]

web代码片段

JS的数据类型判断函数数组对象结构处理日期转换函数,浏览器类型判断函数合集

如何最快在N个有序的整数数组中取得最大K个数?

android 如何获取保存的图片的地址 并存到数据库中