[英]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.