4

const char を char に変換しようとしています...ここに私のコードがあります:

bool check(const char* word)
{

char *temp[1][50];

temp = word;

return true;
}

const char が渡された関数です。その const char を char に変換する必要があります。このコードを実行すると、コンパイラは次のエラーをスローします。

dictionary.c:28:6: error: array type 'char *[1][50]' is not assignable
temp = word;

この変換を正しく完了するにはどうすればよいですか?

ありがとう、ジョシュ

4

2 に答える 2

5
#include <string.h>
bool check(const char* word)
{
    char temp[51];
    if(strlen(word)>50)
        return false;
    strncpy(temp,word,51);
    // Do seomething with temp here
    return true;
}
于 2013-04-13T16:15:40.773 に答える
1

const 以外のバージョンが必要な場合は、文字列をコピーする必要があります。

char temp[strlen(word) + 1];
strcpy(temp, word);

または:

char * temp = strdup(word);

if(!temp)
{
    /* copy failed */

    return false;
}

/* use temp */

free(temp);
于 2013-04-13T16:15:53.807 に答える