[英]C pointer to structure with elements that are pointers - how to allocate memory and use as function output
If I have the following C struct: 如果我具有以下C结构:
struct group_of_pointers {
double *p1, *p2, *p3;
} *pointers;
Now, I want to allocate n spaces (each with sizeof double) to pointers->p1, pointers->p2, pointers->p3. 现在,我想为指针-> p1,指针-> p2,指针-> p3分配n个空间(每个空间的大小为double)。 How do I do this? 我该怎么做呢? Do I have to allocate any space to the pointer to the structure 'pointers' itself? 我是否必须为指向结构“指针”本身的指针分配空间?
Thanks. 谢谢。
ETA: Related question: The reason I need this struct is because I want to return 3 variable length arrays from a function. 预计到达时间:相关问题:我需要此结构的原因是因为我想从一个函数返回3个可变长度的数组。 Should I just do 我应该做吗
void foo(const double* const input, double *output1, double *output2, double *output3)
or should I do 还是我应该做
struct group_of_pointers *foo(const double* const input)
The former looks a bit confusing with input and output all bundled together. 前者看起来与捆绑在一起的输入和输出有些混乱。 But is it just the way C is? 但这仅仅是C的方式吗?
Yes, you have to allocate space for the structure of pointers first. 是的,您必须首先为指针的结构分配空间。 Then you allocate for each component. 然后为每个组件分配。
pointers = malloc(sizeof *pointers);
pointers->p1 = malloc(n * sizeof(double));
pointers->p2 = malloc(n * sizeof(double));
pointers->p3 = malloc(n * sizeof(double));
Yes, since your pointers
variable is itself a pointer, you need to give it some memory to point to. 是的,因为你的pointers
变量本身是一个指针,你需要给它一些内存指向。
struct group_of_pointers {
double *p1, *p2, *p3;
} *pointers;
pointers = malloc(sizeof(*pointers));
pointers->p1 = malloc(n*sizeof(*pointers->p1));
pointers->p2 = malloc(m*sizeof(*pointers->p2));
pointers->p3 = malloc(o*sizeof(*pointers->p3));
If I'm reading your question correctly, yes, you need the 'pointers' struct to have space, either allocated or on the stack, and you malloc()-or-variant-thereof the space for p1 - p3: 如果我正确地阅读了您的问题,是的,您需要“指针”结构来分配或在堆栈上分配空间,并且您为p1-p3分配了malloc()或-variant-space:
struct group_of_pointers *pointer = malloc(sizeof(*pointer));
pointer->p1 = malloc(n * sizeof(double));
pointer->p2 = malloc(n * sizeof(double));
pointer->p3 = malloc(n * sizeof(double));
/* do stuff here... */
free(pointer->p1);
free(pointer->p2);
free(pointer->p3);
free(pointer);
Of course, I never checked for malloc failure in there, so this code is not to be considered production-ready! 当然,我从未在那里检查过malloc失败,因此该代码不被认为可以投入生产!
What are you trying to do? 你想做什么? Do need a struct that can point to three double values? 是否需要一个可以指向三个double值的结构? Then yes, you need to allocate memory for any of those objects: 然后,是的,您需要为任何这些对象分配内存:
#include <stdio.h>
#include <stdlib.h>
struct group_of_pointers {
double *p1, *p2, *p3;
} *pointers;
int main(int argc, char *argv[]) {
pointers = malloc(sizeof(struct group_of_pointers));
pointers->p1 = malloc(sizeof(double));
pointers->p2 = malloc(sizeof(double));
pointers->p3 = malloc(sizeof(double));
*(pointers->p1) = 10.0;
*(pointers->p2) = 20.0;
*(pointers->p3) = 30.0;
printf("%lf %lf %lf\n", *(pointers->p1), *(pointers->p2), *(pointers->p3));
return EXIT_SUCCESS;
}
if those three arrays are highly related, and you use them often as a group you should use the second approach, but instead of creating a structure on the heap you should create it on the stack then pass its pointer to your function, like 如果这三个数组高度相关,并且您经常将它们作为一个组使用,则应使用第二种方法,但是与其在堆上创建结构,不如在堆栈上创建结构,然后将其指针传递给函数,例如
void foo(struct group_of_pointers *ptr, const double *input); void foo(struct group_of_pointers * ptr,const double * input);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.