Needed: A function which returns two different values (int,str)
需要:返回两个不同值的函数(int,str)
So foo calculates smth. and stores the addresses of the two values in the return array.
所以foo计算smth。并将两个值的地址存储在返回数组中。
Now I want to read the values back into variables of these types.
现在我想将值读回到这些类型的变量中。
void** foo(){
void** bar = malloc(2 * sizeof(void*));
...
return bar;
}
int main(int argc, char* argv[]){
void** result = foo();
int* val1 = (int*)result[0];
char* val2 = (char*)result[1];
}
This snippet is not compiling.
此代码段未编译。
Excuse me:
the problem was not the code but that I declared it in a case branch without any compution instructions.
问题不是代码,而是我在没有任何计算指令的案例分支中宣布它。
int main(int argc, char* argv[]){
switch(xyz){
case a:
void** result = foo();
int* val1 = (int*)result[0];
char* val2 = (char*)result[1];
break;
}
}
Which seems not to make sense in a case.
在一个案例中似乎没有意义。
Thanks though.
谢谢。
2 个解决方案
#1
3
If you come up with the need to use a void**
, it is a strong indication of poor program design. Just forget about this idea. void
pointers in general is something you should avoid.
如果你想出使用void **的话,那就是程序设计不佳的强烈迹象。忘了这个想法。一般来说,void指针是你应该避免的。
What you actually want to do seems to be something like this:
你真正想做的事情似乎是这样的:
typedef struct
{
int i;
char c;
} something_t;
something_t* foo (void)
{
something_t* result = malloc (sizeof(*result));
...
return result;
}
int main(int argc, char* argv[]){
something_t* result = foo();
int* val1 = &result->i;
char* val2 = &result->c;
}
If the struct members need to be pointers, simply change the struct.
如果struct成员需要是指针,只需更改结构。
#2
1
You should use structure to do your job
你应该用结构来完成你的工作
#include <stdio.h>
#include <stdlib.h>
#define STRING_WIDTH 32
struct test
{
int val1;
char *val2;
};
struct test *foo(void)
{
struct test *bar = malloc(sizeof(struct test));
if (bar != NULL)
{
bar->val1 = 0;
bar->val2 = calloc (STRING_WIDTH, 1);
if (bar->val2 == NULL)
return NULL;
}
return bar;
}
int main( void )
{
struct test *result = foo();
if (result != NULL)
{
// USE YOUR STRUCT
}
return 0;
}
Take note that malloc
ated memory must be free
d
请注意,必须释放mallocated内存
#1
3
If you come up with the need to use a void**
, it is a strong indication of poor program design. Just forget about this idea. void
pointers in general is something you should avoid.
如果你想出使用void **的话,那就是程序设计不佳的强烈迹象。忘了这个想法。一般来说,void指针是你应该避免的。
What you actually want to do seems to be something like this:
你真正想做的事情似乎是这样的:
typedef struct
{
int i;
char c;
} something_t;
something_t* foo (void)
{
something_t* result = malloc (sizeof(*result));
...
return result;
}
int main(int argc, char* argv[]){
something_t* result = foo();
int* val1 = &result->i;
char* val2 = &result->c;
}
If the struct members need to be pointers, simply change the struct.
如果struct成员需要是指针,只需更改结构。
#2
1
You should use structure to do your job
你应该用结构来完成你的工作
#include <stdio.h>
#include <stdlib.h>
#define STRING_WIDTH 32
struct test
{
int val1;
char *val2;
};
struct test *foo(void)
{
struct test *bar = malloc(sizeof(struct test));
if (bar != NULL)
{
bar->val1 = 0;
bar->val2 = calloc (STRING_WIDTH, 1);
if (bar->val2 == NULL)
return NULL;
}
return bar;
}
int main( void )
{
struct test *result = foo();
if (result != NULL)
{
// USE YOUR STRUCT
}
return 0;
}
Take note that malloc
ated memory must be free
d
请注意,必须释放mallocated内存