c - 在循环中动态分配结构内存 t 次而不是声明结构数组

标签 c arrays pointers malloc structure

原始代码(使用结构数组): 在这段代码中,我将 t 作为用户的输入并声明一个结构数组 tc大小为 t,然后进行一些处理。

#include<stdio.h>
int main()
{
    int t,i,j,k,min=0;
    //# of test cases
    scanf("%d",&t);

    struct testcase
    {
        int sizeOfArray;
        int a[10];
        int b[10];
        int ans;
    };

    struct testcase tc[t];            //declaring array of structures, size t 

    for(i=0;i<t;i++)
        {
            scanf("%d",&tc[i].sizeOfArray);  //entering size of a and b
            for(j=0;j<tc[i].sizeOfArray;j++)   //entering elements of a
                    scanf("%d",&(tc[i].a[j]));

            for(j=0;j<tc[i].sizeOfArray;j++)   //entering elements of b
                    scanf("%d",&tc[i].b[j]);                    
        }
    int no=0;
    for(k=0;k<t;k++)
        {
            min=    tc[k].a[0]+tc[k].b[1];
            for(i=0;i<tc[k].sizeOfArray;i++)
                {
                    for(j=0;(j<tc[k].sizeOfArray);j++)
                        {
                            if((tc[k].a[i]+tc[k].b[j]<min)&&(j!=i))                        
                                    min=tc[k].a[i]+tc[k].b[j];
                        }
                }
            tc[k].ans=min;
            printf("%d\n",min);
        }
    return 0;
}

我尝试过的: 这里我没有声明大小为 t 的结构数组,而是在 for 循环中动态分配结构内存并进行相同的处理。

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int t,i,j,k,min=0;
    //# of test cases
    scanf("%d",&t);
    struct testcase
    {
        int sizeOfArray;
        int a[10];
        int b[10];
        int ans;
  };

    struct testcase *tc = NULL;          

    for(i=0;i<t;i++)
        {
            struct testcase* tc = malloc(20 * sizeof(*tc));
            scanf("%d",&tc[i].sizeOfArray);  //entering size of a and b
            for(j=0;j<tc[i].sizeOfArray;j++)   //entering elements of a
                    scanf("%d",&(tc[i].a[j]));

            for(j=0;j<tc[i].sizeOfArray;j++)   //entering elements of b
                    scanf("%d",&tc[i].b[j]);                   
        }
    int no=0;
    for(k=0;k<t;k++)
        {
            min=tc[k].a[0]+tc[k].b[1];
            for(i=0;i<tc[k].sizeOfArray;i++)
                {
                    for(j=0;(j<tc[k].sizeOfArray);j++)
                        {
                            if((tc[k].a[i]+tc[k].b[j]<min)&&(j!=i))
                                    min=tc[k].a[i]+tc[k].b[j];
                        }
                }
            tc[k].ans=min;
            printf("%d\n",min);
        }
    return 0;
}

问题: 为什么第二个代码不起作用?需要做哪些更正,在第二个代码中,我是否正确使用了 mallocmalloc 是否正确放置?或有任何语法错误或逻辑错误吗?

最佳答案

你需要动态分配一个struct testcase数组,所以你只需要在进入循环之前做一次。

struct testcase *tc = malloc( t * sizeof(struct testcase));
if (!tc) {
    perror("malloc failed");
    exit(1);
}

for(i=0;i<t;i++)
...

关于c - 在循环中动态分配结构内存 t 次而不是声明结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41683146/

相关文章:

arrays - 为什么@($null) 是$false,而@($null, $null) 是$true?

javascript - 如何比较两个字符串数组,不区分大小写且与排序无关 - JS,ES6

c - 返回值在 printf() 格式中不起作用

c - 我的c代码无法按顺序运行

c - Linux 中对 pthread_create 的 undefined reference

PHP - 将两个数组(相同长度)合并为一个关联?

c - C中的指针输出

c - 如何将自定义参数传递给 linux 定时器的函数?

c - 这段代码中的 fgets 有什么错误?

c - x = (char *)&a 的作用是什么,它有什么作用?