我有一个函数需要:一个包含字符的数组、一个有限数组(空)和一个数值变量。包含字符的数组包含一个单词,该函数逐个字符读取该单词并删除附近的相同字符 (abbca -> abca),数字变量读取最终单词中的字符数。我的代码使用两个这样的函数,我需要比较两个最终变量,但是当它们显示在屏幕上时,它们等于零,但它们应该是字符数之和。
void FuncKeyword(char array[], char highlighted_word[], int count) {
for(int index = 0; index < MAXOP && array[index] != 0; index++)
{
if (index > 0 && array[index] == array[index-1])
{
continue;
}
else
{
highlighted_word[count] = array[index];
count++;
}
}
printf("%d", count);
}
我需要使用第二个数组显示第一个数组的计数值。我听说可以用积分来完成,但我不知道怎么做,谁能帮忙?
这是我写的代码:
void FuncKeyword(char array[], char highlighted_word[], int count) {
for(int index = 0; index < MAXOP && array[index] != 0; index++)
{
if (index > 0 && array[index] == array[index-1])
{
continue;
}
else
{
highlighted_word[count] = array[index];
count++;
}
}
printf("%d", count);
}
int main()
{
//scan_words();
char TEST_ARRAY[] = {"qweqq"};
char word1[MAXOP]
int index1 = 0;
FuncKeyword(TEST_ARRAY, word1, index1);
printf(" %d ", index1);
}
我想从打印中获取值“5”,但我得到的值是“0”。
您通过值而不是通过引用传递索引参数,以便在函数内部您可以从外部获得该值的副本,就像局部变量一样,并且从外部无法得知其更改。阅读有关参数传递的内容
或者通过指针 (
*) 传递,(在 C++ 中可以通过引用&),或者,如果这是函数的主要结果,则将其设为其类型int而不是void,并将其作为返回值返回这是更正后的代码: