Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

c - 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:

#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
", word + 1);
   
}
int main()
{
    int len, i;
    char sentence[100];
    char temp;
    char *ptr = sentence, **ptr_double = &ptr;
    printf("Enter a sentence
");
    fflush(stdin);
    gets(*ptr_double);
    puts(*ptr_double);
    len = strlen(*ptr_double);
    word_counter(ptr_double, len);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

ptr_double is a char**. That means *ptr_double is a char*.

So in *(ptr_double+i) != ' ' you are comapring a char* to an int (the ' ').

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...