繁体   English   中英

我正在尝试使用双指针将字符串传递给 function 但出现错误

[英]I am trying to pass a string into a function with a double pointer but getting an error

所以我想传递一个字符数组的双指针,但我收到了这个错误:“警告:指针和 integer 之间的比较 if (*(ptr_double+i):= ' ' && *(ptr_double+i+1) = ='')" 这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void word_counter(char **ptr_double, int len)
{
    int i = 0, j = 0, count = 0, word = 0;
    for (i = 0; i < len; i++)
    {   
        if (*(ptr_double+i) != ' ' && *(ptr_double+i+1) == ' ')
            word = word + 1;
    }word++;
    printf("The number of words is %d\n", word + 1);
   
}
int main()
{
    int len, i;
    char sentence[100];
    char temp;
    char *ptr = sentence, **ptr_double = &ptr;
    printf("Enter a sentence\n");
    fflush(stdin);
    gets(*ptr_double);
    puts(*ptr_double);
    len = strlen(*ptr_double);
    word_counter(ptr_double, len);
}

ptr_double是一个char** 这意味着*ptr_double是一个char*

因此,在*(ptr_double+i) != ' '中,您将char*int' ' )相匹配。

使用多级指针时,请始终确保您知道要取消引用的内容。

你真正想要的是*(*ptr_double+i)(*ptr_double)[i]因为*ptr_double实际上指向你想要索引的字符串。

暂无
暂无

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

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