[英]I am trying to pass a string into a function with a double pointer but getting an error
So I want to pass the double pointer of a character array, but I am getting this error: "warning: comparison between pointer and integer if (*(ptr_double+i):= ' ' && *(ptr_double+i+1) == ' ')" Here is my code:所以我想传递一个字符数组的双指针,但我收到了这个错误:“警告:指针和 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
is a char**
. ptr_double
是一个char**
。 That means *ptr_double
is a char*
.这意味着
*ptr_double
是一个char*
。
So in *(ptr_double+i) != ' '
you are comapring a char*
to an int
(the ' '
).因此,在
*(ptr_double+i) != ' '
中,您将char*
与int
( ' '
)相匹配。
When using multiple level of pointers always make sure you know what you are dereferencing.使用多级指针时,请始终确保您知道要取消引用的内容。
What you actually want is *(*ptr_double+i)
or (*ptr_double)[i]
as *ptr_double
actually points to the string you want to index.你真正想要的是
*(*ptr_double+i)
或(*ptr_double)[i]
因为*ptr_double
实际上指向你想要索引的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.