简体   繁体   English

如何修复 - 从 'char *' 分配给 'char' 使得 integer 从没有强制转换的指针

[英]How to fix - assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast

I need to remove all the commas from my user input.我需要从我的用户输入中删除所有逗号。 The code is working but it's giving the warning "assignment to 'char' from 'char *' makes integer from pointer without a cast".该代码正在运行,但它给出了警告“从'char *'分配给'char'使得integer从指针没有强制转换”。 I need to get rid of this warning.我需要摆脱这个警告。

removeCommas(argv[1]);

printf("%s", argv[1]);

return 0;



    for(i=0; i<strlen(num); i++)
    {   
        c = *(num + i);
        if(c == 44)    
        {   
            *(num + i) = "";                                               
        }                        
    }       

return 0;
        *(num + i) = "";                                               

I'm not sure what you are trying to assign here, but double quotes ( "" ) denotes char * .我不确定您要在这里分配什么,但双引号( "" )表示char *

If you wanted to remove , (ASCII 44) from the string it is not the way to do it.如果您想从字符串中删除, (ASCII 44) 这不是这样做的方法。 You need to shift all the chars after , until \0 and terminate the string properly.您需要在 , 之后移动所有字符,直到\0并正确终止字符串。

You should work on a char by char basis.您应该逐个字符地工作。 loop over your input string, search for the comma, and then override it with the remaining of the string:循环输入字符串,搜索逗号,然后用剩余的字符串覆盖它:

#include "string.h"
#include "stdio.h"



int main()

{
    char num[100];
    strcpy(num, "abc,def,,a,");

    unsigned int i;
    char c;

    for (i = 0; i < strlen(num); i++)
    {
        c = num[i];
        if (c == ',')
        {
            if (num[i + 1] == '\0')
                num[i] = '\0';
            else
            {               
                size_t s = strlen(num + i + 1);
                memmove(num + i, num + i + 1, s+1);

                --i;
            }
        }
    }

    printf("%s", num);

    return 0;
}

Note that (if you are going to remove all comas from string), the resulting string may be shorter than source one.请注意(如果您要从字符串中删除所有逗号),生成的字符串可能比源字符串短。 And there is no real "string" type in C - it's only array of chars. C 中没有真正的“字符串”类型 - 它只是字符数组。

I think, that proper way will be:我认为,正确的方法是:

  1. Count number of commas in source string计算源字符串中的逗号数
  2. Calculate length of resulting string and reserve enough memory for it计算结果字符串的长度并为其预留足够的 memory
  3. Iterate throug characters of source string, and copy them to resulting string, if they are not commas遍历源字符串的字符,如果它们不是逗号,则将它们复制到结果字符串

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 &#39;char *&#39; 对 &#39;char&#39; 的赋值使指针从没有强制转换的整数 [-Wint-conversion] - assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion] 从'const char *'分配给'char'使得integer从指针没有强制转换 - assignment to 'char' from 'const char *' makes integer from pointer without a cast 从 char * 对 char 的赋值从没有大小写的指针中生成整数 - assignment to char from char * makes integer from pointer without a case 我是 C 的初学者,我的代码出现错误:从 char * 分配给 char 使得 integer 从没有演员表的指针。 我该如何解决这个问题? - Im a beginner to C and getting an error with my code: assignment to char from char * makes integer from pointer without a cast. How could I fix this? 从 &#39;char *&#39; 初始化 &#39;char&#39; 从指针生成整数而不进行强制转换 - initialization of ‘char’ from ‘char *’ makes integer from pointer without a cast 当将char指针分配给字符数组时,赋值使整数指针不带强制转换[默认启用] - assignment makes pointer from integer without a cast [enabled by default] when assigning char pointer to character array 为什么这是“从‘int’到‘char *’的赋值使来自 integer 的指针没有转换 [-Wint-conversion]” - Why this is "assignment to ‘char *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]" 错误:赋值使指针从整数变成整数,但没有强制转换[-Werror] str1 =(unsigned char *)s1 - error: assignment makes integer from pointer without a cast [-Werror] str1 = (unsigned char*)s1 C 多维字符数组 - 赋值从指针生成整数而不进行强制转换 - C multidimentional char array - assignment makes integer from pointer without a cast 解引用char *时,收到“赋值使指针从整数开始而没有强制转换”警告 - Getting a “assignment makes pointer from integer without a cast” warning when dereferencing char *
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM