c - 如何将项目动态添加到动态数组

标签 c pointers dynamic-arrays

我正在尝试使用函数将项添加到动态数组中。我要做的是计算数组的大小并将其增加一个。我的问题是计算数组的大小;sizeof(*tab) / sizeof((*tab)[0])总是为零。

struct table
{
    char key[20];
    int val;
};

struct table *symTab, *insTab, *datTab;

void addFieldToTable(struct table **tab, char key[MAX_KEY_LENGTH], int val) {
    struct table temp;
    strcpy(temp.key, key);
    temp.val = val;

    size_t space;
    if (*tab == 0) {
        space = 1;
        *tab = malloc(space);
        *tab[0] = temp;
        return;
    }
    //#region for test only
    size_t sizeOfTab = sizeof(*tab);//4
    size_t sizeOfEntity1 = sizeof((*tab)[0]);//24
    size_t sizeOfEntity2 = sizeof(**tab);//24
    size_t sizeOfEntity3 = sizeof(struct table);//24
    //#endregion
    space = sizeof(*tab) / sizeof((*tab)[0]);//always gets zero
    space++;
    *tab = realloc(*tab, space);
    *tab[space - 1] = temp;

}

int main()
{
    addFieldToTable(&insTab, "mov", 0);
    addFieldToTable(&insTab, "cmp", 1);
}

在我的代码中,第一个项添加成功,但是当尝试添加第二个项时,计算的大小为零,因此数组不会调整大小,因此第一个项*tab[0]将被新项替换。
如何解决计算数组大小以使函数能够动态添加项的问题?.

最佳答案

您正在为具有24B+填充的结构分配1B。
你要做的是为你的sizeof分配内存。

*tab = malloc(sizeof(struct table));

尺寸
sizeof(*tab); // Is just size of pointer

sizeof(**tab); === sizeof(struct table);  // It's size of Structure

如果您想知道数组有多少个元素,请传递另一个包含该信息的参数。
void addFieldToTable(struct table **tab, char key[MAX_KEY_LENGTH], int val, int noElements)

int noElements = 0;
addFieldToTable(&insTab, "mov", 0, noElements++);
addFieldToTable(&insTab, "cmp", 1, noElements++);
addFieldToTable(&insTab, "third", 1, noElements++);

重新分配是昂贵的操作,例如,您应该为100个结构分配空间,然后,如果需要更多空间,则为200个结构重新分配空间。
如果你搞不懂,你的代码会被更正的
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_KEY_LENGTH 20
struct table
{
    char key[MAX_KEY_LENGTH];
    int val;
};

struct table *symTab, *insTab, *datTab;

void addFieldToTable(struct table **tab, char key[MAX_KEY_LENGTH], int val, int noElements) {
    struct table temp;
    strcpy_s(temp.key, key);
    temp.val = val;

    if (*tab == NULL)
    {
        *tab = malloc(sizeof(struct table));
        (*tab)[0] = temp;
        return;
    }

    *tab = realloc(*tab, ((noElements + 1) * sizeof(struct table)));

    (*tab)[noElements] = temp;
}

int main()
{
    int noElements = 0;
    addFieldToTable(&insTab, "mov", 0, noElements++);
    addFieldToTable(&insTab, "cmp", 1, noElements++);
    addFieldToTable(&insTab, "third", 1, noElements++);

    for (int i = 0; i < noElements; i++)
    {
        printf("\n%d: %s, %d", i+1, insTab[i].key, insTab[i].val);
    }

    return 0;
}

输出
1: mov, 0
2: cmp, 1
3: third, 1

关于c - 如何将项目动态添加到动态数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45603960/

相关文章:

c++ - 函数返回的增量指针

c++ - 更改数组类以保存动态数组

c - 混合语言CUDA编程

python - C : What assumptions can be made about the value of the underlying variable? 中的枚举

c - 如何使用scanf获取换行符?

c++ - C++ 中的 C 字符串删除

c++ - 尝试将单词插入 trie 时出现段错误

c++ - 指针导致访问冲突

C语言: How to use memset to reset dynamic 2d array?

javascript - 检索数组JS中的所有对象