【发布时间】:2025-12-21 02:45:12
【问题描述】:
我已经定义了一个vector 的指针与工作日。稍后,我想对日期进行排序lexicographically。
我使用printf("%s", *(wochentag + i)); 打印所有工作日。我想要每个工作日的每个第一个字母的输出。稍后,我还想访问每个单词的第二、第三、...字母。
通过使用
printf("%c", *(wochentag + i));`
我收到以下警告:
format specifies type 'int' but the argument has type 'char *' [-Wformat].
这是我的代码:
int main()
{
static char *wochentag[] = {"Montag", "Dienstag", "Mittwoch", "Donnerstag",
"Freitag", "Samstag", "Sonntag"};
printf("%lu\n", sizeof(wochentag));
printf("%lu\n", sizeof(char));
for (int i = 0; i < 7; i++)
{
printf("%s\n", *(wochentag + i));
}
for (int i = 0; i < 7; i++)
{
printf("%c\n", *(wochentag + i));
}
}
我做错了什么?
【问题讨论】:
-
格式字符 '%c' 表示一个字符(或字节),可以表示为 int,但实际上您是在取消引用 printf("%s\n", *( wochentag + i));
-
请不要使用
*(arr + i)访问数组的元素。请改用arr[i]。
标签: c string pointers char printf