gpt4 book ai didi

c - *日期 = "Sunday"; vs int *数字= 7;

转载 作者:太空宇宙 更新时间:2023-11-04 06:49:57 25 4
gpt4 key购买 nike

我有这样一段代码:

#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.

我的代码有什么问题?

最佳答案

应该是

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 处的整数值。

关于c - *日期 = "Sunday"; vs int *数字= 7;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52913360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com