TestStruct.rar

C# 结构体教程
本文提供了一篇关于 C# 中结构体使用的详细介绍,并附带了 VisualStudio2008 版本的源码示例,帮助读者更好地理解和使用 C# 的结构体。
【认知结构(C# Struct)】文章附带源码(Visual Studio 2008 版本)

fj.pngTestStruct.rar

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/14325734/viewspace-548755/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/14325734/viewspace-548755/

#include <stdio.h> #include <stdlib.h> #include <string.h> #include "cJSON.h" enum TEST_ENUM { ENUM1 = 0, ENUM2, ENUM3 }; struct TestStruct { int testId; enum TEST_ENUM enum_value; }; char* parser_TestStruct(cJSON* json_root,int* size) { if (!json_root) { printf("json is NULL \n"); return NULL; } // s2j_create_struct_obj(,struct TestStruct); struct TestStruct test_struct; char* buffer = NULL; test_struct.testId = cJSON_GetObjectItem(json_root, "testId")->valueint; test_struct.enum_value = cJSON_GetObjectItem(json_root, "enum_value")->valueint; // 获取二级命令码 *size = sizeof(struct TestStruct); printf("testId is %d\n",test_struct. testId); printf("enum_value is %d\n",test_struct . enum_value); buffer = (char*)malloc(sizeof(struct TestStruct)); memset(buffer,0,sizeof(struct TestStruct)); // buffer = (char*)(&test_struct); memcpy(buffer,&test_struct,sizeof(struct TestStruct)); printf("\nIn Function %s,buffer is: [",__func__); for(int i = 0; i < *size; i++) { printf("%02X ",buffer[i]); } printf("]\n"); return buffer; //这里不进行free,由调用者执行接口以后,自行释放 } int main() { // 创建并设置缓冲区 //char* myBuffer = SetBuffer(); char* json_str = "{\"testId\":2,\"enum_value\":4}"; cJSON* json_root = cJSON_Parse(json_str); char* buffer = NULL; int size = -1; buffer = parser_TestStruct(json_root,&size); printf("\nIn Function %s,buffer is: [",__func__); for(int i = 0; i < size; i++) { printf("%02X ",buffer[i]); } printf("]\n"); return 0; } 我想将这个代码中,让函数parser_TestStruct位于其他的.c文件,且必须要保证功能正确
07-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值