gpt4 book ai didi

c - 为什么要打印两个指针减去警告的结果?

转载 作者:太空宇宙 更新时间:2023-11-04 00:34:07 26 4
gpt4 key购买 nike

<分区>

我正在尝试减去 2 个指针,以便它们给出元素的数量。我可以编译程序并运行它。但是编译后它会抛出错误

pointerarithmetic.c: In function ‘main’:
pointerarithmetic.c:9:8: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat=]
printf("%d",(q-p));

代码:

#include<stdio.h>

int main(){
int a[5]={0,10,20,30,40};
int *p,*q;
p=&a[0];
q=&a[2];
printf("%d",*p);
printf("%d",*q);
printf("%d",(q-p));
return 0;
}

预期的输出应该是元素的数量。

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