1

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++;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Destin Hilt
  • 15
  • 1
  • 7
  • it says its the error is on the line where I call insert(A, "1", "a"); – Destin Hilt Aug 04 '19 at 23:10
  • 4
    C and C++ are different languages. Out of interest, what sort of debugger is it that works better on C++ code than C code? – M.M Aug 04 '19 at 23:13
  • 3
    Using a C++ compiler to compile C code is going to lead you into all sorts of problems which would not manifest themselves if you use a C compiler instead. They are different languages. C and Modern C++ are very different languages. You're seeing an example of the difference between C and C++. – Jonathan Leffler Aug 04 '19 at 23:14
  • You can't compile C code with a C++ compiler. Whatever it compiles is **C++**. – Antti Haapala -- Слава Україні Aug 04 '19 at 23:32

1 Answers1

2

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.

Alexander Solovets
  • 2,447
  • 15
  • 22