在使用 C++ 编译器编译代码时将 C 字符串文字传递给函数时遇到问题?

Trouble with passing C string literals to a function while using a C++ compiler to compile the code?

我收到一条错误消息,指出 "argument type "const char *" 与 "char *" 不兼容。此代码是由我的教授提供的,我不确定是什么问题是。

我正在编写 C,但我使用的是 C++ 编译器,因为如果重要的话,它更容易调试。

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++;
}

C++ 中的字符串文字始终是 const char[N] 类型,其中 N 是包含(或不包含)空终止字节的字符串的大小。它们也可以被隐式强制为 const char * - 因此你关于不兼容类型的错误。有关详细信息,请参阅此 answer