简体   繁体   English

动态更改C中数据结构元素的数组大小

[英]Dynamically changing the array size of an element of data structure in C

I am new to C programming. 我是C编程新手。 Please excuse if this question is not appropriate. 如果这个问题不合适,请原谅。 I have been struggling to dynamically change the size of a variable inside the structure (not the struct itself). 我一直在努力动态地更改结构内部变量的大小(而不是结构本身)。 Let's say I have a struct called dist1 like the code below. 假设我有一个名为dist1的结构,如下面的代码。 I want to pass this structure into a function and dynamically change the size of test1. 我想将此结构传递给函数并动态更改test1的大小。 Is this even possible? 这有可能吗?

#include <stdio.h>
struct distance
{
    double *test1;
    double *test2;
};


int main()
{
    struct distance dist1;
    add(&dist1); 

    return 0;
}

void add(struct distance *d3) 
{
     // I want to dynamically change the size of "test1" to let's say test1(50)
     // Is this possible?
}

This isn't possible in any meaningful way. 这不可能以任何有意义的方式实现。 You can always have struct distance container pointers to double instead of doubles, and then change the size of the pointed to memory, but your goal here isn't clear so I'm not sure what use that would be. 您总是可以使struct distance容器的指针变为双精度而不是双精度,然后更改指向内存的大小,但是您的目标尚不清楚,因此我不确定该使用什么。

The members of struct distance are best first initialized. 最好首先初始化struct distance的成员。

struct distance dist1 = { NULL, NULL };

To change the the number of elements allocated, use realloc() . 要更改分配的元素数,请使用realloc() Pass to it d3->test1 and the number of bytes needed. 将其传递给d3->test1和所需的字节数。 If the value returned is not NULL , the re-allocation succeeded and code should use that. 如果返回的值不为NULL ,则重新分配成功,并且代码应使用该值。 Research realloc() for more details. 研究realloc()以获得更多详细信息。

#include <stdlib.h>
void add(struct distance *d3) {
  // I want to dynamically change the size of "test1" to let's say test1(50)
  size_t new_element_count = 50;
  void *tmp = realloc(d3->test1, sizeof *(d3->test1) * new_element_count);
  if (tmp == NULL && new_element_count > 0) {
    Handle_OutOfMemory();
  } else {
    d3->test1 = tmp;
  }
}

I was able to finally get this run. 我终于可以运行了。 I really appreciate everybody's help and time. 我非常感谢大家的帮助和时间。 You guys are amazing! 你们很棒!

#include <stdio.h>
#include <stdlib.h>

struct distance
{
    double *test1;
    double *test2;
};


void add(struct distance *dist1) ;


int main()
{
    struct distance dist1;

    dist1.test1 = (double *) malloc(5*sizeof(double));
    dist1.test1[4] = 14.22;
    printf("dist1.test1[4] from main() = %f\n", dist1.test1[4]);

    add(&dist1); 

    printf("dist1.test2[3] from main() = %f\n", dist1.test2[3]);

    return 0;
}

void add(struct distance *dist1) 
{
     printf("dist1.test1[4] from add() = %f\n", (*dist1).test1[4]);

     (*dist1).test2 = (double *) malloc(10*sizeof(double));
     (*dist1).test2[3] = 14.67;
     printf("dist1.test2[3] from add() = %f\n", (*dist1).test2[3]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM