#Why does passing char array not work but passing char* work for this:

24 messages · Page 1 of 1 (latest)

cedar wyvern
#
static void initialize_state_id_to_token_map_for_dfa(Lexer *lexer, const char *id_to_token_type_path) {
    FILE *fp = fopen(id_to_token_type_path, "r");
    if (!fp) { report_file_error(id_to_token_type_path); }
    HashMap *string_to_token_map = lexer->string_to_token_type_map;
    int BUF_SIZE = MAX_TOKEN_SIZE;
    char buffer[BUF_SIZE];
    DFA *dfa = lexer->dfa;
    for (int i = 0; i < dfa->N_STATES; ++i) {
        fgets(buffer, BUF_SIZE, fp);
        buffer[strcspn(buffer, "\r\n")] = 0;
        // THIS LINE: &buffer does not work but changing buffer to dynamic memory         // and then doing &buffer works
        int *token = get_from_hashmap(string_to_token_map, &buffer);
        if (token == NULL) {
            fprintf(stderr, "Invalid token type %s\n", buffer);
            exit(1);
        }
        dfa->states[i].token_associated = *token;
    }
}

This is the function causing the error;

// hash function will receive pointer to string aka pointer to char*
size_t hash_string_djb2(const void *key) {
    char **str_ptr = (char **) key;
    const char *str = *str_ptr;
    size_t hash = 5381;
    int c;
    while (true) {
        c = (unsigned char) *str++;
        if (c == 0) return hash;
        hash = ((hash << 5) + hash) + c;
    }
}
bold estuaryBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

cedar wyvern
#

It works when I change the first code to:

static void initialize_state_id_to_token_map_for_dfa(Lexer *lexer, const char *id_to_token_type_path) {
    FILE *fp = fopen(id_to_token_type_path, "r");
    if (!fp) { report_file_error(id_to_token_type_path); }
    HashMap *string_to_token_map = lexer->string_to_token_type_map;
    int BUF_SIZE = MAX_TOKEN_SIZE;
    char *buffer = calloc_safe(BUF_SIZE, sizeof(char));
    DFA *dfa = lexer->dfa;
    for (int i = 0; i < dfa->N_STATES; ++i) {
        fgets(buffer, BUF_SIZE, fp);
        buffer[strcspn(buffer, "\r\n")] = 0;
        int *token = get_from_hashmap(string_to_token_map, &buffer);
        if (token == NULL) {
            fprintf(stderr, "Invalid token type %s\n", buffer);
            exit(1);
        }
        dfa->states[i].token_associated = *token;
    }
}
primal pumice
cedar wyvern
#

oh

#

So I should do &buffer[0] right?

#

no

#

I mean, there's no way to pass address of the array? i.e. char**?

primal pumice
#

&buffer[0], buffer, and &buffer all result in a pointer represented by the same address

#

what does the hashmap actually store though? I'm assuming it's a hash map of char*

#

and so that's why the hash function would cast to char**

#

in that case, all of the pointers you store in the hashmap need to be dynamically allocated

cedar wyvern
#

it's a get call

#

so for get it doesn't matter

#

is there any way to pass the address of the buffer array?

primal pumice
#

if it doesn't matter, then you could pass &&*buffer or &&buffer[0]

cedar wyvern
#

okay

primal pumice
#

oh wait that doesn't work because it's a temporary pointer

#

so

char *str = buffer;
pass(&str);
cedar wyvern
#

okay

#

BTW, is storing pointers to constant memory okay in the hashmap?

#

Like I have constant strings array

#

I am storing pointers from that array in the hashmap

primal pumice
#

yeah, it's fine