[英]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:我认为,正确的方法是:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.