Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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
C只打印数组的第一个字符,不打印其余字符吗?_C_Arrays_String_Pointers_Memory - Fatal编程技术网

C只打印数组的第一个字符,不打印其余字符吗?

C只打印数组的第一个字符,不打印其余字符吗?,c,arrays,string,pointers,memory,C,Arrays,String,Pointers,Memory,基本上,我必须标记一个4列的行,并将这些标记放入一个数组中,因此我在下面创建了这个函数 char** tokeniser(char* lineToToken) { int i = 0; char** tokenList = malloc(4*sizeof(char*)); char* token; while ((token = strtok(lineToToken, " ")) != NULL && i<4) { t

基本上,我必须标记一个4列的行,并将这些标记放入一个数组中,因此我在下面创建了这个函数

char** tokeniser(char* lineToToken)
{
    int i = 0;
    char** tokenList = malloc(4*sizeof(char*));
    char* token;
    while ((token = strtok(lineToToken, " ")) != NULL && i<4)
    {
        tokenList[i] = malloc(strlen(token) + 1);
        strcpy(tokenList[i], token);
        ++i;
    }
    return tokenList;
}
char**tokeniser(char*lineToToken)
{
int i=0;
char**tokenList=malloc(4*sizeof(char*));
字符*令牌;

而((token=strtok(lineToToken,”)!=NULL&&i问题在于您对strtok()的使用。

在第一次调用中,函数需要一个C字符串作为str的参数,str的第一个字符用作扫描标记的起始位置。在后续调用中,函数需要一个空指针,并使用最后一个标记结束后的位置作为扫描的新起始位置

总之,您正在一次又一次地传递要标记化的字符串,而不是仅在第一次传递它(以及
NULL
后续时间)

因此,以下程序可能会为您提供所需的示例:

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

char** tokeniser(char* lineToToken)
{
    int i = 0;
    char** tokenList = (char**) malloc(4 * sizeof(char*));
    char* token = strtok(lineToToken, " ");
    while(token != NULL && i < 4)
    {
        tokenList[i] = (char*) malloc(strlen(token) + 1);
        strcpy(tokenList[i], token);
        token = strtok(NULL, " ");
        ++i;
    }
    return tokenList;
}

int main(int argc, char const* argv[])
{
    char str[] = "asda asdasd 23 asd";
    char** tokenList = tokeniser(str);

    for(int i = 0; i < 4; ++i)
    {
        printf("%s\n", tokenList[i]);
    }
    return 0;
}

问题在于您如何使用strtok()

在第一次调用中,函数需要一个C字符串作为str的参数,str的第一个字符用作扫描标记的起始位置。在后续调用中,函数需要一个空指针,并使用最后一个标记结束后的位置作为扫描的新起始位置

总之,您正在一次又一次地传递要标记化的字符串,而不是仅在第一次传递它(以及
NULL
后续时间)

因此,以下程序可能会为您提供所需的示例:

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

char** tokeniser(char* lineToToken)
{
    int i = 0;
    char** tokenList = (char**) malloc(4 * sizeof(char*));
    char* token = strtok(lineToToken, " ");
    while(token != NULL && i < 4)
    {
        tokenList[i] = (char*) malloc(strlen(token) + 1);
        strcpy(tokenList[i], token);
        token = strtok(NULL, " ");
        ++i;
    }
    return tokenList;
}

int main(int argc, char const* argv[])
{
    char str[] = "asda asdasd 23 asd";
    char** tokenList = tokeniser(str);

    for(int i = 0; i < 4; ++i)
    {
        printf("%s\n", tokenList[i]);
    }
    return 0;
}

在上述函数中,每次Strtok函数都传递相同字符串的起始地址

通常,strtok函数应按以下方式调用

#include<stdio.h>
#include<string.h>
void main() {
    char Src[25]="Hare Krishna Hare Rama";
    char C[2]=" ";
    char *del=C;
    char *temp[5];
    int  i=0;
    temp[i] = strtok(Src,del);
    while(temp[i] !=NULL) {

        printf("The str is <%s\n>",temp[i]);
        temp[++i] = strtok(NULL,del);

    }

}
#包括
#包括
void main(){
char Src[25]=“Hare Krishna Hare Rama”;
字符C[2]=“”;
char*del=C;
字符*温度[5];
int i=0;
温度[i]=标准温度(Src,del);
while(temp[i]!=NULL){
printf(“str是”,temp[i]);
温度[++i]=strtok(NULL,del);
}
}
第一次调用时,必须传递字符串和分隔符的起始地址。
然后strtok返回指向分隔符的起始指针。因此,下次调用时,无需传递字符串的起始地址,strtok将记住指向分隔符下一个字符的地址。因此,后续调用应使用空指针进行调用。

在上述函数中,每次传入strtok函数时g相同字符串的起始地址

通常,strtok函数应按以下方式调用

#include<stdio.h>
#include<string.h>
void main() {
    char Src[25]="Hare Krishna Hare Rama";
    char C[2]=" ";
    char *del=C;
    char *temp[5];
    int  i=0;
    temp[i] = strtok(Src,del);
    while(temp[i] !=NULL) {

        printf("The str is <%s\n>",temp[i]);
        temp[++i] = strtok(NULL,del);

    }

}
#包括
#包括
void main(){
char Src[25]=“Hare Krishna Hare Rama”;
字符C[2]=“”;
char*del=C;
字符*温度[5];
int i=0;
温度[i]=标准温度(Src,del);
while(temp[i]!=NULL){
printf(“str是”,temp[i]);
温度[++i]=strtok(NULL,del);
}
}
第一次调用时,必须传递字符串和分隔符的起始地址。
然后strtok返回指向分隔符的起始指针。因此,下次调用时,无需传递字符串的起始地址,strtok将记住指向分隔符下一个字符的地址。因此后续调用应使用空指针进行调用。

这不是
strtok()的方式
工作。这不是
strtok()
的工作方式。
tokenList
i<4
时未完全初始化。可能在(i@chux很好,虽然这不是我个人写东西的方式!想到这里也有硬编码
4
,我也不寒而栗。是的,最好传入
char**tokeniser(char*lineToToken,size\u t sz)
如果
i<4
,则令牌列表未完全初始化。可以在(i@chux很好,虽然这不是我个人写东西的方式!想到这里也有硬编码
4
,我也不寒而栗。是的,最好传入
char**tokeniser(char*lineToToken,size\u t sz)