简体   繁体   English

如何使用结构 || 在 C 中创建一个 NULL(s) 的 2D char* 数组错误:“{”标记之前的预期表达式

[英]How to make a 2D char* array with NULL(s) in C with a struct || error: expected expression before '{' token

I want to make a hashtable in c, where the keys are integers and the values are strings我想在 c 中创建一个哈希表,其中键是整数,值是字符串

I have a 2D char array in a struct, as follows in hashtable.h file:我在结构中有一个二维字符数组,在 hashtable.h 文件中如下所示:

#ifndef hashtable
#define hashtable

// define the maxmium size
#define INITIAL_SIZE 5
#define LOAD_FACTOR 0.7

typedef struct hashtable
{
    int* keyArray[INITIAL_SIZE]; 
    char* valueArray[INITIAL_SIZE][101]; // strings have maximum of 100 chars
    bool isActiveArray[INITIAL_SIZE]; // for deleting elements

    int count;
    int capacity;
    double loadFactor;

    // true: linear probing, false: quadratic probing
    bool collisionHandler;

} table;

#endif

I am trying to initialize the values of the arrays outside of the struct, like so我正在尝试在结构之外初始化 arrays 的值,就像这样

void initTable(table* p) {
    // constructor
    p->count = 0;
    p->capacity = INITIAL_SIZE;
    p->loadFactor = LOAD_FACTOR; 
    p->collisionHandler = true;
    p->keyArray = {NULL};
    p->valueArray = {{NULL}};
    p->isActiveArray = {false};
} 

however i receive these errors:但是我收到这些错误:

In file included from HashTable.c:22:0:
functions.h: In function 'initTable':
functions.h:85:16: error: expected expression before '{' token
  p->keyArray = {NULL};
                ^
functions.h:86:18: error: expected expression before '{' token
  p->valueArray = {{NULL}};
                  ^
functions.h:87:21: error: expected expression before '{' token
  p->isActiveArray = {false};

note: p is a pointer to my table struct注意: p 是指向我的表结构的指针

I want to know how to make a 2D array of all NULL values in the char* array, like {{NULL}, {NULL}, {NULL}} also to use for comparison later on like trying to insert a value into the valueArray, and checking if null我想知道如何制作 char* 数组中所有 NULL 值的二维数组,例如{{NULL}, {NULL}, {NULL}}也用于稍后进行比较,例如尝试将值插入 valueArray ,并检查 null

I also want to make the keyArray, the int* list to be like {NULL, NULL, NULL} instead of a random memory address so I can easily check for a NULL pointer, and then replace it with a pointer to an int when making a new key/value pair我还想让 keyArray,int* 列表像 {NULL, NULL, NULL} 而不是随机的 memory 地址,这样我就可以轻松地检查 Z6C3E226B4D4795D518AB341B082 地址时的 Z6C3E226B4D4795D518AB341B082 指针,然后用指向 intEC 指针的指针替换它一个新的键/值对

To initialise a region of memory (an instance of a struct either as a local variable or from the heap) where most elements are NULL (or false), simply use:要初始化 memory 的区域(作为局部变量或来自堆的结构的实例),其中大多数元素是 NULL(或 false),只需使用:

table t;
memset( &t, 0, sizeof t );

Then go on to initialise the few elements that are not NULL or 0 or false:然后打开 go 以初始化不是 NULL 或 0 或 false 的少数元素:

#include <assert.h>

void initTable( table* p ) {
        assert( p != NULL );

        memset( p, 0, sizeof *p );
        p->capacity = INITIAL_SIZE;
        p->loadFactor = LOAD_FACTOR; 
        p->collisionHandler = true;
} 

Kudos for using "include guards" in the header file.感谢在 header 文件中使用“包含防护”。 More conventional would be using an UPPERCASE token, and perhaps even a suffix...更传统的是使用大写标记,甚至可能是后缀......

#ifndef HASHTABLE_H

One further suggestion:还有一个建议:

char* valueArray[INITIAL_SIZE][100 + 1];

makes it clear (to me) that the '\0' is being considered in this allocation. (对我来说)清楚地表明在此分配中正在考虑 '\0'。

Initializing the values of an array using the { } brace syntax will only work in a declaration.使用{ }大括号语法初始化数组的值只会在声明中起作用。 You cannot use that syntax after an array has already been declared.在声明数组后,您不能使用该语法。

If you really want to use the brace syntax after an array has already been declared, one thing you can do is declare another temporary array of the same type and size and initialize it with the brace syntax, and then copy the temporary array to the original array using memcpy , for example like this:如果你真的想在声明数组后使用大括号语法,你可以做的一件事是声明另一个相同类型和大小的临时数组并使用大括号语法对其进行初始化,然后将临时数组复制到原始数组使用memcpy的数组,例如这样:

#include <stdio.h>
#include <string.h>

int main( void )
{
    //declare array without initializing it
    int arr[5];

    //create temporary array and initialize it with brace syntax
    const int temp[5] = { 1, 2, 3, 4, 5 };

    //copy temporary array to original array
    memcpy( arr, temp, sizeof arr );

    //print contents of original array
    for ( int i = 0; i < 5; i++ )
    {
        printf( "%d\n", arr[i] );
    }
}

This program will output the numbers 1 to 5 .这个程序将 output 数字15

Using this method, your function initTable would look like this:使用此方法,您的 function initTable将如下所示:

void initTable( table* p ) {
    // constructor
    p->count = 0;
    p->capacity = INITIAL_SIZE;
    p->loadFactor = LOAD_FACTOR;
    p->collisionHandler = true;

    const int* temp_keyArray[INITIAL_SIZE] = { NULL };
    memcpy( p->keyArray, temp_keyArray, sizeof temp_keyArray );

    const char* temp_valueArray[INITIAL_SIZE][101] = { NULL };
    memcpy( p->valueArray, temp_valueArray, sizeof temp_valueArray );

    const bool temp_isActiveArray[INITIAL_SIZE] = { false };
    memcpy( p->isActiveArray, temp_isActiveArray, sizeof temp_isActiveArray );
}

However, since you want to initialize all array elements to zero, there is a simpler solution.但是,由于您想将所有数组元素初始化为零,因此有一个更简单的解决方案。 You can use the function memset to set all bytes of the array to zero:您可以使用 function memset将数组的所有字节设置为零:

void initTable( table* p ) {
    // constructor
    p->count = 0;
    p->capacity = INITIAL_SIZE;
    p->loadFactor = LOAD_FACTOR;
    p->collisionHandler = true;
    memset( p->keyArray, 0, sizeof p->keyArray );
    memset( p->valueArray, 0, sizeof p->valueArray );
    memset( p->isActiveArray, 0, sizeof p->isActiveArray );
}

Or, as already pointed out in the other answer by someone else, you can set all bytes of the entire struct hashtable to zero using memset and afterwards only set the values of the individual members of the struct which are not supposed to be zero.或者,正如其他人在另一个答案中已经指出的那样,您可以使用memset将整个struct hashtable的所有字节设置为零,然后只设置struct中不应为零的各个成员的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM