Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用c语言在可变长度表中存储可变长度数据_C_Oop_Data Structures - Fatal编程技术网

用c语言在可变长度表中存储可变长度数据

用c语言在可变长度表中存储可变长度数据,c,oop,data-structures,C,Oop,Data Structures,我必须存储如下所示的数据-> 我需要像set\u data(data\u id,index,data)这样的接口在表中写入数据,并get\u data(data\u id,index)从表中读取数据 我必须用c代码实现这一点 需要一些逻辑帮助来编写代码 如何使用c中的结构实现 想要实现这样的东西 struct INDEX { U8 Len; U8 data[]; }; struct DATA_ID { struct INDEX index[]; }; struct

我必须存储如下所示的数据->

我需要像
set\u data(data\u id,index,data)
这样的接口在表中写入数据,并
get\u data(data\u id,index)
从表中读取数据

我必须用c代码实现这一点

需要一些逻辑帮助来编写代码

如何使用c中的结构实现

想要实现这样的东西

struct INDEX
{
    U8 Len;
    U8 data[];
};

struct DATA_ID
{
    struct INDEX index[];
};

struct DATA_ID data_id[max];
数据[]
每个索引和
索引[]
每个数据id的长度不同

对于需要使用malloc的动态内存分配

给你一个提示:每次你想初始化一个未知长度(大小)的数据类型时,最好使用malloc。唯一的另一种解决方案是,使用足够大的数据类型来初始化未知的数据类型,但这只会浪费内存,而且通常还会浪费性能

抄袭


你知道怎么写吗,或者这是一个“请给我代码”的问题?了解C语言的动态内存分配会对你有所帮助。只需实现为一个单一的结构(
struct collection{size\t group\u id,index;double data};
),然后根据需要分配一个大的块,例如,
struct collection*mycollection=malloc(多少个*sizeof*mycollection))malloc调用的强制转换没有任何作用。您应该在malloc中添加一个sizeof(char),以避免不同系统出现问题。这是一个教程,其目的很简单。顺便说一句,malloc的目的是分配足够的内存来存储字符串str,长度为15字节。是的,但在某些系统上,char可能不是直接的1字节,因此您应该在malloc调用中添加sizeof。我也没有说malloc是没有目的的,但它的号召是有目的的。
#include <stdio.h>
#include <stdlib.h>

int main () {
   char *str;

   /* Initial memory allocation */
   str = (char *) malloc(15);
   strcpy(str, "tutorialspoint");
   printf("String = %s,  Address = %u\n", str, str);

   /* Reallocating memory */
   str = (char *) realloc(str, 25);
   strcat(str, ".com");
   printf("String = %s,  Address = %u\n", str, str);

   free(str);
   
   return(0);
}
String = tutorialspoint, Address = 355090448
String = tutorialspoint.com, Address = 355090448