繁体   English   中英

在c中的不同函数中修改字符串数组

[英]Modifying an array of strings in a different function in c

我正在尝试将一个字符串数组传递给另一个函数并在那里对其进行修改。 这是我声明数组和其他函数的地方。 (实际上,我正在做的是获取一串字符,并将它们分类为字符串数组中的单词,并丢弃空格)。 数组的大小仅仅是由于我正在处理的说明。 “strInput”是我将要“清理”的大量字符

char cleaned[151][21];
cleanInput(strInput, &cleaned);

后来我声明:

void cleanInput(char* s, char* cleaned[151][21])
{
  //do stuff
}

这是给我一个警告。

warning: passing argument 2 of ‘cleanInput’ from incompatible pointer 
type [-Wincompatible-pointer-types]
cleanInput(strInput, &cleaned);


note: expected ‘char * (*)[21]’ but argument is of type ‘char (*)[151][21]’
void cleanInput(char* s, char* cleaned[151][21]);

我尝试了几种不同的传递方法,但是从我所看到的,我传递了一个指向二维数组的指针,并且它要求一个指向二维数组的指针。 我不确定为什么它无效。

void cleanInput(char* s, char (*cleaned)[21])
{
    // stuff
}

并像这样称呼它

cleanInput("", cleaned);

但是您现在可能不知道您有多少行。 我会做

void cleanInput(char* s, char (*cleaned)[21], size_t rows)
{
    for(size_t i = 0; i < rows; ++rows)
    {
        if(**(cleaned + i) != 0)
            printf("word on row %zu: %s\n", i, *(cleaned + i));
    }
}

并称之为:

char cleaned[151][21];
memset(cleaned, 0, sizeof cleaned);
strcpy(cleaned[0], "hello");
strcpy(cleaned[1], "world");
strcpy(cleaned[23], "Bye");
cleanInput("", cleaned, sizeof cleaned / sizeof *cleaned);

这输出:

word on row 0: hello
word on row 1: world
word on row 23: Bye
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM