简体   繁体   English

传递参数 1 从指针生成 integer 而没有对 putchar 的强制转换警告

[英]Passing Argument 1 makes integer from pointer without a cast warning for putchar

I'm studying an exercise of making a simple code that has the function that acts the same way as printf using a variadic function .我正在研究制作一个简单代码的练习,该代码具有 function,其行为方式与使用可变参数 function 的printf 相同

Also, I wish to use sprintf and putchar, and not printf.另外,我希望使用 sprintf 和 putchar,而不是 printf。

The code seems to act and give the results as it should do (ie print a character for %c, and an integer for %d).代码似乎按照应有的方式运行并给出结果(即为 %c 打印一个字符,为 %d 打印一个 integer)。

However, I keep getting a warning message for putchar(cStr);但是,我不断收到关于putchar(cStr);的警告消息 that says Passing Argument 1 of 'putchar' makes integer from pointer without a cast .上面写着Passing Argument 1 of 'putchar' makes integer from pointer without a cast

Also, I get a note that says expected int but argument is of type char * .另外,我收到一条注释,上面写着expected int but argument is of type char *

I've tried to get this solved by changing putchar(cStr);我试图通过更改putchar(cStr);来解决这个问题into putchar(*cStr);放入putchar(*cStr); , but then the code gives me a segment fault . ,但代码给了我一个segment fault

What should I do to get the warning message and note solved?我应该怎么做才能解决警告消息和注意事项?

#include <stdio.h>
#include <string.h>
#include <stdarg.h>

void new_printf(const char*format, ...);

int main(void)
{
    new_printf("A single character : %c An integer : %d \n", 'T', 37);
    return 0;
}

void new_printf(const char *format, ...)
{
    va_list ap;
    char ary[50];
    char *cStr;
    int iNum, i=0;
    va_start(ap, format);
    while(*format)
    { 
        if(strncmp(format, "%d",2) == 0){
            iNum = va_arg(ap, int);
            sprintf(ary, "%d", iNum);
            while(ary[i]!='\0'){
                putchar(ary[i]);
                i++;
            }       
            format = format + 2;
        }
        else if(strncmp(format, "%c", 2)==0)
        {
            cStr = va_arg(ap, char*);
            putchar(cStr);
            format = format + 2;
        }
        else{
            putchar(*format);
            format++;
        }
    }
    va_end(ap);
}

The correct results would be as follows:正确的结果如下:

A single character : T An integer : 37

In the call new_printf("A single character: %c An integer: %d \n", 'T', 37);在调用new_printf("A single character: %c An integer: %d \n", 'T', 37); , 'T' is passed for the %c conversion. , 'T'%c转换传递。 'T' is a character: It is a small integer value. 'T'是一个字符:它是一个小的 integer 值。 It is not a pointer.它不是指针。 For historic reasons, its type is int .由于历史原因,它的类型是int This is a correct thing to pass for %c .这是为%c传递的正确内容。

Under else if(strncmp(format, "%c", 2)==0) , in cStr = va_arg(ap, char*);else if(strncmp(format, "%c", 2)==0)下,在cStr = va_arg(ap, char*); , you tell va_arg to give you the next argument and that that argument is a char * . ,你告诉va_arg给你下一个参数,那个参数是一个char * It is not.它不是。 It is an int .它是一个int

You should retrieve it with va_arg as an int and assign it to an int , not to char * , or simply use it directly as an int .您应该使用va_arg作为int检索它并将其分配给int ,而不是char * ,或者直接将其用作int

You should use int x = va_arg(ap, int); putchar(x);你应该使用int x = va_arg(ap, int); putchar(x); int x = va_arg(ap, int); putchar(x); or simply putchar(va_arg(ap, int));或者简单地putchar(va_arg(ap, int)); . .

暂无
暂无

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

相关问题 警告:传递&#39;calcFx&#39;的参数2将使指针产生整数而不进行强制转换 - warning: passing argument 2 of 'calcFx' makes integer from pointer without a cast 警告:传递&#39;strcpy&#39;的参数1会使指针从整数开始而不进行强制转换 - warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast 传递参数 1 从指针生成整数而没有强制转换警告 - Passing Argument 1 makes integer from pointer without a cast warning 警告:传递“ quicksort”的参数1会使指针从整数开始而无需强制转换 - warning: passing argument 1 of 'quicksort' makes pointer from integer without a cast 警告:传递&#39;memcpy&#39;的参数1会使指针从整数开始而无需强制转换 - warning: passing argument 1 of ‘memcpy’ makes pointer from integer without a cast 警告传递&#39;finder&#39;的参数2使指针从整数开始而无需强制转换 - Warning passing argument 2 of 'finder' makes pointer from integer without a cast 警告:传递&#39;fopen&#39;的参数2会使指针从整数开始而无需强制转换 - warning: passing argument 2 of ‘fopen’ makes pointer from integer without a cast c程序,&#39;fstat&#39;的警告消息传递参数1从没有强制转换的指针生成整数 - c program, warning message passing argument 1 of ‘fstat’ makes integer from pointer without a cast 警告:传递&#39;pthread_join&#39;的参数1将使指针产生整数,而没有强制转换错误 - warning: passing argument 1 of ‘pthread_join’ makes integer from pointer without a cast error 传递sprintf的参数2使指针从整数开始而无需强制转换。 C警告 - Passing argument 2 of sprintf makes pointer from integer without a cast. C warning
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM