gpt4 book ai didi

c - 指向 char 的指针 - 增量运算符

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

<分区>

恐怕我没有正确理解运算符之间的优先级。以下程序的工作方式与预期不同:

#include<stdio.h>

int main(){
char a[] = "abc";
char *p;
int i;

p = a;
for(i = 0; i < 3; ++i) /* First case */
printf("%c\n", *p++);

printf("a = %s\n", a);

p = a;

for(i = 0; i < 3; ++i) /* Second case */
printf("%c\n", (*p)++);
printf("a = %s\n", a);

return 0;
}

第一种情况:根据这个table , *p++*(p++)。我递增 p 指向的地址,然后我取消引用它。为什么它返回 abc,而不是 bc + "value beyond string"

我在第二种情况下遇到了类似的问题,表达式为 (*p)++。在这种情况下,括号使我增加了 p 的去引用值。因此,我希望表达式返回 bcd 而不是 dbc

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