将一个void指针投射到一个结构上,我想初始化结构组件。我使用下面的代码。 我想初始化并访问test-> args结构。我该怎么做?将一个void指针投射到一个结构并初始化它
#include <string.h>
#include <stdio.h>
#include<stdlib.h>
struct ctx {
int a;
int b;
void *args;
};
struct current_args {
char *a;
int b;
};
int main()
{
struct ctx current_ctx = {0};
struct ctx *test=¤t_ctx ;
struct current_args *args = (struct current_args *)(test->args);
args->a=strdup("test");
args->b=5;
printf("%d \n", (test->args->b));
return 0;
}
什么是你的问题? – haccks
那么,你*做*指针,但它已经初始化为'NULL',所以取消引用它是未定义的。 – EOF