How to solve the problem of incompatibility const char* c argument of type char* in VS?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
56 views
In the code the hash table I for some reason an error is thrown in Maine, specifically
int main() { struct listnode *node; hashtab_init(hashtab); hashtab_add(hashtab, "Tigr", 190); // error on "Tigr" hashtab_add(hashtab, "Slon", 2300); // error on "Slon" hashtab_add(hashtab, "Volk", 60); // error on "Volk" node = hashtab_lookup(hashtab, "Slon"); // error on "Slon" printf("Node: %s, %d\", node->key, node->value); return 0; }


In code blocks it works.

Error (active) E0167 argument of type "const char *" is incompatible with parameter of type "char *"

the code itself.
#include #include #include #include "stdafx.h" #include #include #define HASHTAB_SIZE 71 #define HASHTAB_MUL 31 struct listnode { char *key; int value; struct listnode *next; }; struct listnode *hashtab[HASHTAB_SIZE]; int hashtab_hash(char *key) { int h = 0; char *p; for (p = key; *p != '\\0'; p++) { h = h * HASHTAB_MUL + (int)*p; } return h % HASHTAB_SIZE; } void hashtab_init(struct listnode **hashtab) { int i; for (i = 0; i < HASHTAB_SIZE; i++) { hashtab[i] = NULL; } } int hashtab_add(struct listnode **hashtab, char *key, int value) { struct listnode *node; int index = hashtab_hash(key); node = (struct listnode *) malloc(sizeof(*node)); if (node != NULL) { node->key = key; node->value = value; node->next = hashtab[index]; hashtab[index] = node; } return NULL; } struct listnode *hashtab_lookup( struct listnode **hashtab, char *key) { int index; struct listnode *node; index = hashtab_hash(key); for (node = hashtab[index]; node != NULL; node = node->next) { if (strcmp(node->key, key) == 0) { return node; } } return NULL; } int main() { struct listnode *node; hashtab_init(hashtab); hashtab_add(hashtab, "Tigr", 190); hashtab_add(hashtab, "Slon", 2300); hashtab_add(hashtab, "Volk", 60); node = hashtab_lookup(hashtab, "Slon"); printf("Node: %s, %d\", node->key, node->value); return 0; } void hashtab_delete(struct listnode **hashtab, char *key) { int index; struct listnode *p, *prev = NULL; index = hashtab_hash(key); for (p = hashtab[index]; p != NULL; p = p->next) { if (strcmp(p->key, key) == 0) { if (prev == NULL) hashtab[index] = p->next; else prev->next = p->next; free(p); return; } prev = p; } }
by | 56 views

2 Answers

0 like 0 dislike
To change the function signature char * to const char *. If I understand correctly, hashtab_lookup searches for a value by key and accordingly there is no need to change it. And even if the code compiles and you try to change the line pointer will be UB.
And it would be good to arrange the code in blocks and put the correct tags to the question(C).
by
0 like 0 dislike
There are three ways.
1. To compile the C compiler, not C++. This works perfectly and gives a warning to return NULL (which, however, is clearly excessive).

2. To put const-correctness. As our structures do not manage the lines, and take someone else's, wherever the keys are used to do const.
struct listnode { const char *key; int value; struct listnode *next; };
And so on, places five on all of program.

3. const_cast(someString). Sometimes you need (for example, to make std::string C++03 and some functions of WinApi), but it's not our business.
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
110,608 questions
257,187 answers
0 comments
40,796 users