I'm getting an error that says "argument type "const char *
" is incompatible with "char *
". This code was provided by my professor and I'm not sure what the problem is.
I'm writing C but I'm using a C++ compiler because it is easier to debug if that matters.
int main() {
int i;
Dictionary A;
char* str;
char* v;
char* k = (char*)calloc(100, sizeof(char));
// create a Dictionary and some pairs into it
A = newDictionary();
insert(A, "1", "a"); // it doesn't like "1" or "a"
// here is the function:
// insert()
// inserts new (key,value) pair into the end (rightmost part of) D
// pre: lookup(D, k)==NULL
void insert(Dictionary D, char* k, char* v) {
Node N, A, B;
if (D == NULL) {
fprintf(stderr,
"Dictionary Error: calling insert() on NULL Dictionary reference\n");
exit(EXIT_FAILURE);
}
if (findKey(D->root, k) != NULL) {
fprintf(stderr,
"Dictionary Error: cannot insert() duplicate key: \"%s\"\n", k);
exit(EXIT_FAILURE);
}
N = newNode(k, v);
B = NULL;
A = D->root;
while (A != NULL) {
B = A;
if (strcmp(k, A->key) != 0) {
A = A->right;
}
}
if (B == NULL) {
D->root = N;
}
else {B->right = N;}
D->numPairs++;
}
String literals in C++ are always of type const char[N]
where N is the size of the string with (or without) the null-terminating byte. They also can be implicitly coerced to const char *
- hence your error about incompatible types. Refer to this answer for more information.