gpt4 book ai didi

改变指针的值

转载 作者:行者123 更新时间:2023-11-30 19:34:21 25 4
gpt4 key购买 nike

我刚刚开始学习C语言,我有一个基本问题。

#include <stdio.h>

int main() {
// These variables have been assigned hidden values:
int secret;
int *another_secret;

// Declare a variable named secret_pt and make it point to secret:

// Add the value at the address pointed to by another_secret to the
// value at the address pointed to by secret_pt.
// Do not change the address assigned to secret_pt, and don't explicitly set secret.

return 0;
}

这是我的方法;

#include <stdio.h>

int main() {
// These variables have been assigned hidden values:
int secret;
int *another_secret;

// Declare a variable named secret_pt and make it point to secret:

// int *secret_pt;
//secret_pt = &secret;
int *secret_pt = &secret;

// Add the value at the address pointed to by another_secret to the
// value at the address pointed to by secret_pt.
// Do not change the address assigned to secret_pt, and don't explicitly set secret.

int *secret = *another_secret;

return 0;
}

但是我遇到了重新定义错误,这是有道理的,但我不知道如何解决它。

最佳答案

您将获得两个地址:secret_ptanother_secret。系统会要求您将它们的内容添加在一起并将结果存储到 secret_pt 指向的任何位置。这恰好是 secret ,但您不应该直接分配给它。

通过使用 * 运算符取消引用指针来访问指针的内容。因此,secret_pt 地址处的值为 *secret_pt,类似地,another_secret 地址处的值为 *another_secret >。您可以将它们添加在一起。

请记住,*secret_pt 只是secret,您可以使用它来存储secret,而无需实际使用名称 secret

关于改变指针的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44035113/

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