我刚开始学习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;
}
但我正在重新定义错误,这是有道理的,但我不知道如何解决它。
答案 0 :(得分:0)
您将获得两个地址,secret_pt
和another_secret
。系统会要求您将其内容添加到一起,并将结果存储到secret_pt
个点中。这恰好是secret
,但您不应该直接分配给它。
通过使用*
运算符解除引用,可以访问指针的内容。因此secret_pt
地址的值为*secret_pt
,同样地址another_secret
的值为*another_secret
。您可以将它们一起添加。
请注意,*secret_pt
只是secret
,您可以使用该事实存储到secret
而不使用名称secret
。
答案 1 :(得分:0)