- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
为什么编译器会提示以下 printf 中的参数类型“char”和转换说明符“s”不匹配?
#include <stdio.h>
#include <stdlib.h>
typedef char * STR; // causes problems in printf below
int main(void) {
struct MyStruct {
STR str;
};
struct MyStruct ms = {"some text"};
printf("%s\n", ms.str);
return (EXIT_SUCCESS);
}
当删除 typedef 时,编译器对相同的代码没有任何提示:
#include <stdio.h>
#include <stdlib.h>
//typedef char * STR; // runs fine without typedef
int main(void) {
struct MyStruct {
char * str; //STR str;
};
struct MyStruct ms = {"some text"};
printf("%s\n", ms.str);
return (EXIT_SUCCESS);
}
注意事项:
系统 = Win 64、NetBeans IDE 8.2、GCC 编译器,无论是使用 Cygwin 还是 MinGW 工具,无论是 32 位还是 64 位,都没有区别。
如果我避免使用 struct 或 typedef,就会消除错误。但是,如所示,只要同时使用 typedef 和 struct,就会出现错误。
在发布之前,我检查了(除其他外)stackoverflow.com/questions/20944784/,它建议使用 const。但是,无论 typedef 是否为
指向常量 char 的指针 (typedef char const * STR);指向 char 的常量指针 (typedef char * const STR);或者指向常量 char 的常量指针 (typedef char const * const STR);
逐字错误消息:参数类型“char”与转换说明符“s”不匹配。
进一步的测试表明 ms.str 确实是预期的类型 char *(例如,正确的 sizeof,将转换说明符交换为“c”以表示 char,如错误消息所示,如果 printf '将指向 char 的指针作为 char 等)
更改 typedef 标识符的名称(例如,从 STR 到 STR_TEST)会导致相同的错误。因此似乎与标准 header 中的定义没有冲突。
最佳答案
在这个问题的评论回声室中,那些参与者和 OP 似乎已经达成共识,这是 NetBeans 的错。
I cannot reproduce your problem on Cygwin x64. Furthermore, "Mismatching the argument type "char" and conversion specifier "s"" doesn't look like a GCC warning, is probably a Netbeans issue. See also this similar Netbeans bug report – RustyX May 26 at 18:51
@BloodyPeasant gcc does not have an error message that says "Mismatching the argument type "char" and conversion specifier "s"." , it may be NetBeans that shows the error message instead of gcc, in which case NetBeans is wrong, and also have quite poor English grammar in its error messages. – nos May 26 at 19:33
@nos Ha, so poor grammar is the telltale sign that it's a NetBeans issue... Looking under the hood of NetBeans, it appears that you and RustyX are correct that this is just an IDE glitch. Thanks, all. – Bloody Peasant May 27 at 2:01
...所以您的问题需要更正:
Why does
the compiler[NetBeans] complain about a mismatched argument type "char" and conversion specifier "s" in the following printf?
编写 NetBeans 的人是会犯错误的人,此错误消息是其中一种错误的体现,即我们通常所说的“错误”、“故障”或“软件故障”的症状。
关于c - 当 char * 被类型定义并通过结构访问时,为什么编译器会发现 char * 和 printf 的转换说明符 "s"不匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44194688/
我想知道以下语句在 C 中会打印什么? printf("hello\n") || (printf("goodbye\n") || printf("world\n")); 我通常习惯于使用“cout”在
这是我目前正在学习的系统编程类(class)的幻灯片: catch_child 是 SIGCHLD 处理程序。输出与代码如何对应?为什么没有打印一些“Child #x started”消息? 最佳答案
这有点像拼图……我刚刚又回到了C,打算这次掌握它。所以我一直在阅读 The C Programming Language ,我得到了这个声明: Among others, printf also re
如何使用 printf 在字符串末尾附加空格? 我找不到任何在右侧附加空格的示例。 A similar question我发现使用 printf改为在字符串的左侧添加空格。 最佳答案 使用负数左对齐(
我想通过 usart 从 stm32f405 注销。 在我的 syscall.c 文件中,我实现了通过 usart 打印的功能: int _write(int file, char *ptr, int
我想定义一个记录器函数,比如 myPutStrLn = putStrLn . (++) "log: " main = do myPutStrLn "hello" 这很好。现在我想用 printf 格式
Printf module API详细介绍了类型转换标志,其中: %B: convert a boolean argument to the string true or false %b: conv
@H2CO3 这是我的主要代码: #pragma OPENCL EXTENSION cl_ amd_ printf : enable #define PROGRAM_FILE "matvec.cl"
Printf module API详细介绍了类型转换标志,其中: %B: convert a boolean argument to the string true or false %b: conv
您可以使用 printf 字段宽度说明符截断字符串: printf("%.5s", "abcdefgh"); > abcde 不幸的是,它不适用于数字(将 d 替换为 x 是相同的): printf(
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 4 年前。 Improve th
我遇到了我见过的最奇怪的错误之一。 我有一个简单的程序,可以打印多个整数数组。 对数组进行排序,然后打印...... place_in_buf(n100, 100); insertion(10
我的程序是每隔一段时间获取文件大小并显示它以记录任何更改。由于某种原因,执行下面的代码挂起,只为我提供了一个光标。没有打印或显示任何内容。 代码: #include #include #inclu
printf("It is currently %s's turn.\n", current->name); 我想知道为什么在 %s 之后打印出额外的换行符。我知道C 中的字符串总是以\0 结尾。没有
这个问题已经有答案了: printf anomaly after "fork()" (3 个回答) fork() in c using printf [duplicate] (2 个回答) 已关闭 9
我对编程很陌生。 我正在尝试编写一个程序,从数组中调用水果的价格。但我希望代码在写价格之前也写水果的名称。如果我键入 2,如何使输出为“Orange price : 10”而不仅仅是 price :
这个问题在这里已经有了答案: How do I print a non-null-terminated string using printf? (2 个答案) 关闭 7 年前。 例如,我有一个字符
我有一个 atmel UC3-L0 和罗盘传感器。现在我安装 AtmelStudio 并将一些演示代码下载到电路板中。但是我不知道演示代码中的函数 printf 会在哪里出现数据。我应该如何获取数据?
我有一个 atmel UC3-L0 和罗盘传感器。现在我安装 AtmelStudio 并将一些演示代码下载到电路板中。但是我不知道演示代码中的函数 printf 会在哪里出现数据。我应该如何获取数据?
嗨,我是 C 世界的新手,我的代码确实有些奇怪。目标是创建一个函数,可以在开头和结尾处用空格和/或制表符修剪字符串。我无法使用字符串库。 问题是我的代码中有一个 printf 只是用于测试,它的工作非
我是一名优秀的程序员,十分优秀!