带结构指针的C结构

带结构指针的C结构,c,C,我试图使用指针而不是大小声明来定义数组,因为我不知道一个映射可能有多少个元素。尝试使用链接列表,但未成功。如果这是一份报告,我很抱歉。我是新手,如果这个问题看起来很愚蠢,请原谅 #include<stdio.h> typedef struct _keyValue { char *key; char *value; } _keyValue; typedef struct _keyValues { /* _keyValue keyValue[5]; - Th

我试图使用指针而不是大小声明来定义数组,因为我不知道一个映射可能有多少个元素。尝试使用链接列表,但未成功。如果这是一份报告,我很抱歉。我是新手,如果这个问题看起来很愚蠢,请原谅

#include<stdio.h>

typedef struct _keyValue
{
    char *key;
    char *value;
} _keyValue;

typedef struct _keyValues
{
    /* _keyValue keyValue[5];  - This works*/
    _keyValue *keyValue;
    int size;
} _keyValues;

_keyValues map;

main()
{
    map.keyValue[0].key     = "Key One";
    map.keyValue[0].value   = "Value One";

    map.keyValue[1].key     = "Key Two";
    map.keyValue[1].value   = "Value Two";

    map.size = 2;

    printf("Key: %s Value: %s", map.keyValue[0].key, map.keyValue[0].value);
}
#包括
typedef结构_键值
{
字符*键;
字符*值;
}_键值;
typedef结构_键值
{
/*_keyValue keyValue[5];-这很有效*/
_keyValue*keyValue;
整数大小;
}_关键价值观;
_键值图;
main()
{
map.keyValue[0].key=“key One”;
map.keyValue[0].value=“值一”;
map.keyValue[1].key=“key二”;
map.keyValue[1].value=“值二”;
map.size=2;
printf(“键:%s值:%s”,map.keyValue[0]。键,map.keyValue[0]。值);
}

map.keyValue
在您的示例中是一个未初始化的指针。您需要使用
malloc

main()
{
    map.keyValue = malloc(sizeof(*map.keyValue) * 2);
    map.size = 2;

    map.keyValue[0].key     = "Key One";
    map.keyValue[0].value   = "Value One";
int newMapSize = ...
_keyValue* temp = realloc(map.keyValue, sizeof(*map.keyValue) * newMapSize);
if (temp == NULL) {
    /* allocation failed.  Handle out of memory error and exit */
}
map.keyValue = temp;
map.size = newMapSize;
// map.keyValue[0..newMapSize-1] are now available
以后可以使用
realloc

main()
{
    map.keyValue = malloc(sizeof(*map.keyValue) * 2);
    map.size = 2;

    map.keyValue[0].key     = "Key One";
    map.keyValue[0].value   = "Value One";
int newMapSize = ...
_keyValue* temp = realloc(map.keyValue, sizeof(*map.keyValue) * newMapSize);
if (temp == NULL) {
    /* allocation failed.  Handle out of memory error and exit */
}
map.keyValue = temp;
map.size = newMapSize;
// map.keyValue[0..newMapSize-1] are now available
#包括
内部主(空){
map.size=2;
map.keyValue=malloc(sizeof(_keyValue)*map.size);
map.keyValue[0].key=“key One”;
map.keyValue[0].value=“值一”;
map.keyValue[1].key=“key二”;
map.keyValue[1].value=“值二”;
printf(“键:%s值:%s”,map.keyValue[0]。键,map.keyValue[0]。值);
返回0;
}
如果我不知道尺寸

#include <stdlib.h>
#include <string.h>

int main(void){
    char buff[128] = "";

    map.size = 5;//decide the size temporarily
    map.keyValue = malloc(sizeof(_keyValue)*map.size);

    int count = 0, retv;
    while(1){
        printf("input key : ");
        retv=scanf(" %127s", buff);
        if(retv != 1 || strcmp(buff, "end")==0) break;
        map.keyValue[count].key = strdup(buff);
        printf("input value : ");
        scanf(" %127s", buff);
        map.keyValue[count].value = strdup(buff);
        ++count;
        if(count == map.size)//full
            map.keyValue = realloc(map.keyValue, sizeof(_keyValue)*(map.size+=5));//increase the size
    }

    int i;
    for(i=0;i<count;++i)
        printf("Key: %s Value: %s\n", map.keyValue[i].key, map.keyValue[i].value);

    //dealloc
    for(i=0;i<count;++i){
        free(map.keyValue[i].key);
        free(map.keyValue[i].value);
    }
    free(map.keyValue);

    return 0;
}
#包括
#包括
内部主(空){
字符buff[128]=“”;
map.size=5;//暂时决定大小
map.keyValue=malloc(sizeof(_keyValue)*map.size);
int计数=0,retv;
而(1){
printf(“输入键:”);
retv=scanf(“%127s”,浅黄色);
如果(retv!=1 | | strcmp(buff,“end”)==0)中断;
map.keyValue[count].key=strdup(buff);
printf(“输入值:”);
扫描频率(“%127s”,浅黄色);
map.keyValue[count].value=strdup(buff);
++计数;
if(count==map.size)//full
map.keyValue=realloc(map.keyValue,sizeof(_keyValue)*(map.size+=5));//增加大小
}
int i;

对于(i=0;i如果你不知道它们会有多大,那么就使用动态分配

如果不知道结构有多大,这里有一个关于如何使用结构的建议:

首先,在代码中包含以下内容:

#include <stdio.h>   /* for printf */ 
#include <string.h>  /* for strcpy, as you cannot directly assign strings to a malloc'd pointer */
#include <stdlib.h>  /* for malloc and free, for managing memory dynamically */
下面是如何使用结构:

map.size = 30; /* decide how many keyValues we will have */

map.keyValue = malloc(sizeof(_keyValue) * map.size);   /* create storage big enough for 30 _keyValue structs
                                                        * malloc will allow you to assign memory to key and treat it as an array
                                                        * malloc assigns memory from the heap
                                                        * equal to the size specified (30), 
                                                        * this can be potentially as large as your computer's memory */

map.keyValue[0].key = malloc(sizeof(char) * key_size); /* let's create a key at position 0 */

strcpy(map.keyValue.key, "some key"); /* copying some values into key */

map.keyValue[0].value = malloc(sizeof(char) * value_size); /* let's create some space for a value for the 0th element */

strcpy(map.keyValue.value, "some value");


... /* you process and work with those values as you see fit */

free(map.keyValue[0]) /* malloc assigned memory needs to be returned to the OS as it's manually managed, 
                       * here we free the element at position 0 we created earlier
                       * if you have more than element here use a loop e.g: 
                       * for (int i = 0; i < map.size; i++) { free(map.KeyValue[i]) }
                       */


free(map.keyValue); /* free the keyValue itself that stored all the keyValue structs*/
map.size=30;/*决定我们将拥有多少个键值*/
map.keyValue=malloc(sizeof(_keyValue)*map.size);/*创建足够大的存储空间,以容纳30个_keyValue结构
*malloc将允许您将内存分配给键,并将其视为一个数组
*malloc从堆中分配内存
*等于规定的尺寸(30),
*这可能与计算机的内存一样大*/
map.keyValue[0].key=malloc(sizeof(char)*key_size);/*让我们在位置0处创建一个键*/
strcpy(map.keyValue.key,“一些键”);/*将一些值复制到键中*/
map.keyValue[0].value=malloc(sizeof(char)*value_size);/*让我们为第0个元素的值创建一些空间*/
strcpy(map.keyValue.value,“某些值”);
…/*您可以按照自己认为合适的方式处理和使用这些值*/
空闲(map.keyValue[0])/*malloc分配的内存需要返回到操作系统,因为它是手动管理的,
*在这里,我们在前面创建的位置0处释放元素
*如果此处有多个元素,请使用循环,例如:
*对于(inti=0;i

一个提示是,不鼓励使用以下划线开头的声明,因为它们是为该语言保留的。

不要使用以下划线开头的名称;它们基本上是为“实现”保留的。另外,请更具体地回答以下问题:“尝试了链表,但未成功”没有提供足够的信息供某人回答您的问题。非常感谢。感谢您的快速回复。如果您打算在realloc内存中使用此方法,则标准实现是在需要更多空间时将元素数加倍。是的,一种常见的方法是每次
realloc()时将存储量加倍
。其他常见的方法是将存储容量增加1.5倍,或增加固定数量或滑动数量的元素。在任何给定情况下,哪种策略最好可能取决于一些因素,例如存储的使用速度、增长速度、元素的大小。在某些情况下,最好的方法可能是使用im执行这些策略中的多个,并在运行时在它们之间进行选择。您建议的代码看起来是正确的,但对发生了什么变化以及为什么会使其对OP更有价值进行了额外解释。如果我事先不知道映射的大小,并使用循环填充值,该怎么办。@MeUnagi you和I(;-p)有一个很好的解释simonc。如果事先不知道大小,您可以通过
realloc
扩展当前的安全区域。