0

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\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);
}
Lodger
  • 15
  • 5
  • Your code doesn't need to use `char** ptr_double` at all. – Dai Dec 30 '20 at 14:18
  • The name of the function: `vowel_counter(ptr_double, len);` is mis-leading. Its seems that it should be called `word_counter` perhaps? – ryyker Dec 30 '20 at 14:21
  • 1
    @ryyker Actually this is a part of bigger question and using double pointers is the requirement of the question that's why I have to use 'em – Lodger Dec 30 '20 at 14:23
  • Hmm - I only get 4 warnings, each for unused variables: `count, j, i, temp`. But the logic in the parse routine could use some work :) – ryyker Dec 30 '20 at 14:28
  • What exactly are you trying to do? What is `ptr_double` in the `word_counter` function supposed to represent? when you dereference `ptr_double + whatever` once, you get back a `char*`, but you need a `char` to compare with another `char`. – Chase Dec 30 '20 at 14:32
  • 1
    the expressions `*(array + index)` and `array[index]` are equivalent. But one is way more readable than the other. – ryyker Dec 30 '20 at 14:34
  • 1
    In this specific code, `ptr_double` is just the address of `ptr`, which means it points to a **single pointer of chars**. `*ptr_double` will give you the value of this pointer. But `(ptr_double+i)` is past the end of `ptr_double`, I think you should be trying to add the `i` to `*ptr_double`, then dereference that to get a character – Chase Dec 30 '20 at 14:34

1 Answers1

2

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.

koder
  • 2,038
  • 7
  • 10
  • @Lodger, check your logic. The given syntax is correct and does access the character you want. – koder Dec 30 '20 at 14:52