-1

I just started learning C language and i have a basic question.

#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;
}

Here is my approach;

#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;
}

But i'm getting redefining error which makes sense but i don't know how to solve it.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Antt
  • 5
  • 2
  • 2
    Reread the error. You already have a variable named `secret`, of type `int` - and now you're declaring a variable named `secret` of type `int *`. – Antti Haapala -- Слава Україні May 17 '17 at 21:38
  • 1
    That's a homework question and it looks more like a problem understanding the text than a programming question. I'd recommend to aask your tutor to explain what he means (although it is quite clear). Note the code invokes undefined behaviour. – too honest for this site May 17 '17 at 21:39
  • @juanpa.arrivillaga. I read it as `*secret_pt += *another_secret`. `secret` is an `int`, not a pointer. – Mad Physicist May 17 '17 at 21:41
  • @MadPhysicist right, I meant `int sum = *secret_pt + *another_secret;` I'm pretty sure the point is to illustrate dereferencing pointers, given the verbiage "Add the *value at the address pointed to by* another_secret" – juanpa.arrivillaga May 17 '17 at 21:42

2 Answers2

0

You are given two addresses, secret_pt and another_secret. You are asked to add their contents together and store the result into whatever secret_pt points to. This happens to be secret, but you are not supposed to assign to it directly.

The contents of a pointer is accessed by dereferencing it using the * operator. So the value at the address of secret_pt is *secret_pt and similarly the value at the address of another_secret is *another_secret. You can add them together.

Keep in mind that *secret_pt is just secret, a fact that you can use to store into secret without actually using the name secret.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
0

Welcome to ODR, Welcome to pointers

A fundamental C++ rule that states that no translation unit shall contain more than one definition of any variable, function, class type, enumeration type or template, and that every program shall contain exactly one definition of every non-inline function or variable. Some definitions may be duplicated in multiple translation units, subject to strict rules.

ISO/IEC 14882-2014. Programming Languages — C++, Fourth Edition. 2014.

Community
  • 1
  • 1
  • A reference to the C standard might be more appropriate here rather than the C++ standard, even if it happens to be a rule common to both. – 8bittree May 18 '17 at 17:15