【IT专家】在C中以void指针的形式存储通用数据
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
本文由我司收集整编,推荐下载,如有疑问,请与我司联系
在C 中以void 指针的形式存储通用数据
在C 中以void 指针的形式存储通用数据[英]Storing generic data in the form of
void pointer in C I am trying to use void pointer to store generic data in C language 我试图使用void 指针来存储C 语言中的通用数据
This is the structure to store generic data type
这是存储通用数据类型的结构
struct Node{ int id; // Id of the node void *data; // Variable Which stores data I am storing data through this method
我通过这种方法存储数据
int graph_setnode_data(graph_t *graph,int id,void *data){ struct Node *node = getnode(graph,id); if(node != NULL){ node- data = data; return 0; return 1; And accessing data through
并通过访问数据
void* graph_getnode_data(graph_t *graph,int id){ struct Node *node = getnode(graph,id); if(node != NULL){ return node; return NULL; And here is how I am using these methods
以下是我使用这些方法的方法
struct Person{ char *name; int age;int main(){ struct Person *person = malloc(sizeof(struct Person)); person- name = “Goutam”;person- age = 21; graph_t *graph = graph_init(2); graph_createpath(graph,0,1); graph_createpath(graph,1,0); graph_setnode_data(graph,0,(void *)person); struct Person *data =(struct Person *) graph_getnode_data(graph,0); printf(“%d\n”,data- age); graph_destroy(graph); return 0; But I am getting the output:
但我得到的输出:
38162448
38162448。