我有这样一段代码:
#include <stdio.h>
int main(void)
{
char *date = "Sunday";
//int *number = 7;
printf("Today is %s, the 7 days of this week", date);
}它按预期工作并打印。
$ ./a.out
Today is Sunday, the 7 days of this week尽管如此,当我评论
#include <stdio.h>
int main(void)
{
char *date = "Sunday";
int *number = 7;
printf("Today is %s, the %d days of this week", date, *number);
}它报告错误:
$ cc draft.c
draft.c:5:10: warning: incompatible integer to pointer conversion initializing 'int *' with an expression of type
'int' [-Wint-conversion]
int *number = 7;
^ ~
1 warning generated.我的代码有什么问题?
发布于 2018-10-21 08:31:49
它应该是
int number = 7;
printf("Today is %s, the %d days of this week", date, number);而不是
int *number = 7;
printf("Today is %s, the %d days of this week", date, *number);在第一个片段中,用7初始化一个整数变量,并打印其值。在第二段中,使用地址7初始化指针,然后打印内存地址7处的整数值。
https://stackoverflow.com/questions/52913360
复制相似问题