![](/img/trans.png)
[英]I have a function that's returning a char* member of a struct. I am trying to call that function and assign a string to it, but am getting an error
[英]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.