简体   繁体   English

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

[英]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.

相关问题 我有一个返回结构的char *成员的函数。 我正在尝试调用该函数并为其分配一个字符串,但出现错误 - 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 为什么在尝试传递数组值时会出现“不兼容的指针类型”? - Why am I getting "incompatible pointer type" when trying to pass array values? 我试图理解的字符串指针行为 - String pointer behaviour I am trying to understand 尝试将结构指针传递给 function 会导致错误 - Trying to pass a struct pointer to a function results in error 我试图通过使用双指针在 C 中实现 bst 插入操作,但在下面的代码中出现分段错误,idk 为什么? - I am trying to implement bst insertion operations in C by using double pointer but getting a segmentation fault in the code below, idk why? 如何将矩阵传递给双指针函数? - How can I pass a matrix to a double pointer function? 我正在尝试了解特定的函数指针和赋值 - I am trying to understand a specific function pointer and assignment 试图将函数指针传递给pthread - trying to pass function pointer to pthread C语言:将Pointer转换为String,我还能得到什么? - C language: Convert Pointer to String, what am I getting back? 将数字传递给期望枚举的函数时,为什么没有得到错误(或至少警告)? - Why am I not getting an error (or at least warning) when I pass a number to a function that is expecting an enum?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM