首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >*日期=“星期日”;vs int *number = 7;

*日期=“星期日”;vs int *number = 7;
EN

Stack Overflow用户
提问于 2018-10-21 08:18:55
回答 1查看 67关注 0票数 0

我有这样一段代码:

代码语言:javascript
复制
#include <stdio.h>
int main(void)
{
    char *date = "Sunday";
    //int *number = 7;

    printf("Today is %s, the 7 days of this week", date);
}

它按预期工作并打印。

代码语言:javascript
复制
$ ./a.out
Today is Sunday, the 7 days of this week

尽管如此,当我评论

代码语言:javascript
复制
#include <stdio.h>
int main(void)
{
    char *date = "Sunday";
    int *number = 7;

    printf("Today is %s, the %d days of this week", date, *number);
}

它报告错误:

代码语言:javascript
复制
$ 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.

我的代码有什么问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-21 08:31:49

它应该是

代码语言:javascript
复制
int number = 7;
printf("Today is %s, the %d days of this week", date, number);

而不是

代码语言:javascript
复制
int *number = 7;
printf("Today is %s, the %d days of this week", date, *number);

在第一个片段中,用7初始化一个整数变量,并打印其值。在第二段中,使用地址7初始化指针,然后打印内存地址7处的整数值。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52913360

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档