gpt4 book ai didi

c - 以下程序的输出

转载 作者:行者123 更新时间:2023-11-30 19:54:02 26 4
gpt4 key购买 nike

当我运行以下程序时,输出为 5。
为什么是5?为什么不是 8?

void *doit(void *vargp) {
int i = 3;
int *ptr = (int*)vargp;
(*ptr)++;
}
int main() {
int i = 0;
pthread_t tid;
pthread_create(&tid, NULL, doit, (void*)&i);
pthread_join(tid,NULL);
i = i + 4;
printf("%d",i);
}

最佳答案

在doit中,i的值在语句处从0增加到1

(*ptr)++

线程完成后,将其增加 4

i = i + 4;

所以值为 5

关于c - 以下程序的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43839751/

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