通过' strcat的参数1'在没有铸件的情况下从整数制成指针;指针和整数之间的比较
C Newbie在这里,我无法弄清楚我的代码有什么问题,请帮助我。 我收到了两个警告,一个在第12行上,另一个在第15行。
第12行:指针和整数
第15行之间的比较:传递“ strcat”的参数1使来自整数的指针在没有演员的情况下从整数中进行了指针,
#include <stdio.h>
#include <string.h>
int main() {
char string[] = {"<h1>heading 1</h1>"};
char string_len = strlen(string);
char new_string;
for (int i = 0; i < string_len; i++) {
if (string[i] !="<" || string[i] !="/" || string[i] !=">") {
strcat(new_string, string[i]);
}
}
printf(new_string);
return 0;
}
我实际上是在试图编码简单的HTML解析器,但一开始就卡住了。
C newbie here, I can't figure out what's wrong with my code please help me out.
I get two warnings, one on line 12 and the other on line 15.
Line 12 : Comparison between pointer and integer
Line 15: passing argument 1 of 'strcat' makes pointer from integer without a cast
#include <stdio.h>
#include <string.h>
int main() {
char string[] = {"<h1>heading 1</h1>"};
char string_len = strlen(string);
char new_string;
for (int i = 0; i < string_len; i++) {
if (string[i] !="<" || string[i] !="/" || string[i] !=">") {
strcat(new_string, string[i]);
}
}
printf(new_string);
return 0;
}
I am actually trying to code a simple HTML Parser, but stuck on the beginning.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
变量
new_string
具有类型char
它只能存储一个字符。
因此,
strcat
的调用是没有意义的。两个参数都有类型
char
,而该函数期望类型char *
的参数,指向字符串。您需要声明一个字符阵列。
而且,如果您要声明一个数组,则要使用此调用,
该数组必须包含一个字符串。此外,而不是使用
printf
的呼叫,最好编写,或者
在此语句中,您将字符与表达式中使用的字符串文字进行比较,将其转换为指针。
您需要使用整数字符常数,而不是字符串文字,
而不是逻辑或操作员,您需要使用逻辑和操作员
The variable
new_string
has the typechar
It can store only one character.
So the call of
strcat
does not make a sense. The both arguments have the type
char
while the function expects arguments of the typechar *
that point to strings.You need to declare a character array.
And if you will declare an array then to use this call
the array must contain a string. Moreover instead of using this call of
printf
it is better to write eitheror
In this if statement you are comparing characters with string literals that used in the expression are converted to pointers.
You need to use integer character constants instead of the string literals
And instead of the logical OR operator you need to use the logical AND operator