Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
&引用;‘;的参数1的类型不兼容;strcpy&x2019&引用;C中的错误_C_String_Struct_Strtok - Fatal编程技术网

&引用;‘;的参数1的类型不兼容;strcpy&x2019&引用;C中的错误

&引用;‘;的参数1的类型不兼容;strcpy&x2019&引用;C中的错误,c,string,struct,strtok,C,String,Struct,Strtok,我正在编写一个程序,其中必须定义以下函数的我自己的版本: int AtoI ( const char * str ); int StrCmp ( const char * str1, const char * str2 ); char * StrCpy ( char * destination, const char * source ); char * StrCat ( char * destination, const char * source ); char * StrChr ( cha

我正在编写一个程序,其中必须定义以下函数的我自己的版本:

int AtoI ( const char * str );
int StrCmp ( const char * str1, const char * str2 );
char * StrCpy ( char * destination, const char * source );
char * StrCat ( char * destination, const char * source );
char * StrChr ( char * str, int character );
在main函数中,我需要声明一个名为wordlist的数组,类型为myWord,大小为20。然后,使用strtok()库函数,从字符串MyString中提取每个单词,并将其存储在wordlist中。但是,我不断收到错误消息:

incompatible type for argument 1 of ‘strcpy’
对于线路:

strcpy(wordlist[i], token);
如何解决此问题?到目前为止,我的情况如下:

#include <stdio.h>
#include <string.h>

struct myWord{
    char word[21];
    int length;
};

int main(void){
    typedef struct myWord myword;
    int i = 0;
    myword wordlist[20];
    char *myString = "the cat in the hat jumped over the lazy fox";

    char *token;
    token = strtok(myString, " ");

    while(myString != NULL){
        strcpy(wordlist[i], token);
        token = strtok(NULL, " ");
        printf("%s\n", wordlist[i]);
        i++;
    }

}
#包括
#包括
结构myWord{
字符字[21];
整数长度;
};
内部主(空){
typedef结构myWord myWord;
int i=0;
myword词表[20];
char*myString=“戴帽子的猫跳过了懒狐狸”;
字符*令牌;
token=strtok(myString,“”);
while(myString!=NULL){
strcpy(单词表[i],标记);
令牌=strtok(空,“”);
printf(“%s\n”,单词列表[i]);
i++;
}
}
已更正的代码为

#include <stdio.h>
#include <string.h>

typedef struct myWord{
    char word[21];
    int length;
}myword;

int main(void){
    int i = 0;
    myword wordlist[20];
    char myString[] = "the cat in the hat jumped over the lazy fox";

    char *token;
    token = strtok(myString, " ");

    while(token != NULL){
        strcpy(wordlist[i].word, token);
        token = strtok(NULL, " ");
        printf("%s\n", wordlist[i].word);
        i++;
    }    
}
#包括
#包括
typedef结构myWord{
字符字[21];
整数长度;
}我的字;
内部主(空){
int i=0;
myword词表[20];
char myString[]=“戴帽子的猫跳过了懒狐狸”;
字符*令牌;
token=strtok(myString,“”);
while(令牌!=NULL){
strcpy(单词表[i]。单词,标记);
令牌=strtok(空,“”);
printf(“%s\n”,字列表[i].word);
i++;
}    
}
  • 结构的C字符串成员是
    word
    ,因此必须将该成员传递给
    strcpy
    printf
  • 您的循环必须检查
    strtok
    返回的
    token
    ,以检查是否到达字符串的末尾
  • strtok
    修改字符串以执行此任务,因此字符串必须可修改,不能使用指向字符串文字的指针
    wordlist[i]
    -->
    wordlist[i]。word
    char*myString
    -->
    char myString[]
    myString!=空
    -->
    令牌!=NULL
    我进行了更正,它在大部分情况下都有效,但在打印所有标记后,它给了我一个分段错误。
    printf(“%s\n”,wordlist[I])-->
    printf(“%s\n”,单词列表[i].word)
    while(myString!=NULL)
    -->
    while(token!=NULL)