通过' strcat的参数1'在没有铸件的情况下从整数制成指针;指针和整数之间的比较

发布于 2025-02-13 08:02:23 字数 618 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海拔太高太耀眼 2025-02-20 08:02:23

变量new_string具有类型char

char new_string;

它只能存储一个字符。

因此,strcat的调用

strcat(new_string, string[i]);

是没有意义的。两个参数都有类型char,而该函数期望类型char *的参数,指向字符串。

您需要声明一个字符阵列。

而且,如果您要声明一个数组,则要使用此调用,

printf(new_string);

该数组必须包含一个字符串。此外,而不是使用printf的呼叫,最好编写,

printf( "%s\n",new_string);

或者

puts(new_string);

在此语句中,您将字符与表达式中使用的字符串文字进行比较,将其转换为指针。

if (string[i] !="<" || string[i] !="/" || string[i] !=">") {

您需要使用整数字符常数,而不是字符串文字,

if (string[i] !='<' || string[i] !='/' || string[i] !='>') {

而不是逻辑或操作员,您需要使用逻辑和操作员

if (string[i] !='<' && string[i] !='/' && string[i] !='>') {

The variable new_string has the type char

char new_string;

It can store only one character.

So the call of strcat

strcat(new_string, string[i]);

does not make a sense. The both arguments have the type char while the function expects arguments of the type char * that point to strings.

You need to declare a character array.

And if you will declare an array then to use this call

printf(new_string);

the array must contain a string. Moreover instead of using this call of printf it is better to write either

printf( "%s\n",new_string);

or

puts(new_string);

In this if statement you are comparing characters with string literals that used in the expression are converted to pointers.

if (string[i] !="<" || string[i] !="/" || string[i] !=">") {

You need to use integer character constants instead of the string literals

if (string[i] !='<' || string[i] !='/' || string[i] !='>') {

And instead of the logical OR operator you need to use the logical AND operator

if (string[i] !='<' && string[i] !='/' && string[i] !='>') {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文