assigning a pointer to multiple numerical values?

I copied this code from a tutorial and got an error.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main ()
{
  int firstvalue, secondvalue;
  int * mypointer;

  mypointer = &firstvalue;
  *mypointer = 10;
  mypointer = &secondvalue;
  *mypointer = 20;
  cout << "firstvalue is " << firstvalue << endl;
  cout << "secondvalue is " << secondvalue << endl;
  return 0;
}

For once I was relieved to get an error because I don't understand why or how you're allowed to assign to numerical values to one pointer. Can someone please explain? I couldn't understand what it said in the tutorial.

The error was

1
2
3
4
5
6
1
1>  test1.cpp
1>test1.obj : error LNK2005: _main already defined in test.obj
1>tester.obj : error LNK2005: _main already defined in test.obj
1>C:\Users\myanme\documents\visual studio 2010\Projects\tester\Debug\tester.exe : fatal error LNK1169: one or more multiply defined symbols found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Last edited on
A pointer is simply a memory address.
At line 9, you're setting the pointer (a location containing an address) to the address for firstvalue.
At line 10, you're saying store 10 into the int pointed at by the address in mypointer. So firstvalue is now 10.
At line 11, you're setting mypointer to the address of secondvalue.
At line 12, you're storing 20 into the int pointed at by the address in mypointer. So now secondvalue is now 20.

The error you posted has nothing to do with your use of pointers. The error is telling you that you have two main functions.


Ah so
I think the source code there is solid.
The pointer does not hold two values at once. Nor does it hold the values 10 or 20 at all or any integer value.
The pointer holds an "adress" to a variable of type int. (Since it was declared as int *)

doing an assignment on *mypointer
is the same as assigning a value on the variable that "mypointer" points to.
"&firstvalue" is the "adress" of "firstvalue"
so:
1
2
mypointer = &firstvalue;
*mypointer = 10;


essentially assigns the value 10 to the integer POINTED TO BY (*) "mypointer"
which is firstvalue.

when you assign a new reference or "adress" to mypointer in line 11, it drops the old adress and holds the new one, "&secondvalue"

pointers can be intimidating at first but they are indispensible and you MUST learn how to use them.. especially helpful to return the pointer to the first element of an array from a function

and btw don't be confused by the *
the * in these two lines means two COMPLETELY different and unrelated things:

1
2
3
4
5
int i_my_number;              // declares an integer
int * p_my_pointer;           // declares a POINTER to an integer
p_my_pointer = &i_my_number;  // assigns a (REFERENCE to my_number) to my_pointer
*p_my_pointer = 10;  // assigns a value of 10 to the integer POINTED TO by my_pointer
cout << i_my_number; // should print number 10 


why they use the same symbol (*) for declaration of a pointer and dereferencing I don't know but it confused me for a long time. Jus lettin ya know.

All that being said the error is involving other files, not the source you posted.. so I'm not gonna be much help there lol sorry.
Last edited on
AbstractionAnon wrote:
At line 12, you're storing 20 into the int pointed at by the address in mypointer. So now secondvalue is now 20.


This line in particular confuses me.
AbstructionAnon wrote:
At line 9, you're setting the pointer (a location containing an address) to the address for firstvalue.
At line 10, you're saying store 10 into the int pointed at by the address in mypointer. So firstvalue is now 10.
At line 11, you're setting mypointer to the address of secondvalue.

All this I'm on board with because to me I'm thinking that both variable names are being assigned to the same numerical value.

cPlusNOOb wrote:

when you assign a new reference or "adress" to mypointer in line 11, it drops the old adress and holds the new one, "&secondvalue"

Then why does what the old address contains still appear in the output if the address was dropped?
@ science man

because you're sending "firstvalue" and "secondvalue" directly to cout

if you replaced these lines:
1
2
cout << "firstvalue is " << firstvalue << endl;
cout << "secondvalue is " << secondvalue << endl;

with these:
1
2
cout << "firstvalue is " << *mypointer << endl;
cout << "secondvalue is " << *mypointer << endl;

you'd get only the value of "secondvalue" because a reference to it is assigned to "mypointer" in line 11 above. It no longer points to "firstvalue" at this time.

To hold two references you need two pointers, like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main ()
{
  int firstvalue, secondvalue;
  int * mypointer_1, mypointer_2;

  mypointer_1 = &firstvalue;
  *mypointer_1 = 10;
  mypointer_2 = &secondvalue;
  *mypointer_2 = 20;
  cout << "firstvalue is " << *mypointer_1 << endl;
  cout << "secondvalue is " << *mypointer_2 << endl;
  return 0;
}

ta da!

also:
All this I'm on board with because to me I'm thinking that both variable names are being assigned to the same numerical value.

Don't think for a second that a pointer has anything to do with the actual "name" of a variable, nor is it a numerical value in the sense that "int" is. You can change the reference a pointer points to (unless you declare it as const) ... actually I think that's kind of the point of the original code up top there, to show you that you can change a pointer's reference.

Just to say it again, the pointer references only one variable at a time in that program. When "mypointer" points to "firstvalue", 10 is assigned to the integer which "mypointer" points to, which is "firstvalue".

1
2
mypointer = &firstvalue;
*mypointer = 10;

Then a reference to secondvalue, "&secondvalue", is assigned to mypointer. then a value of 20 is assigned to the integer which mypointer points to NOW which is "secondvalue".

1
2
mypointer = &secondvalue;
*mypointer = 20;

..it's just like making two assignments to any variable:

1
2
3
int quant;
quant = 10;
quant = 20;

At no point would "quant" hold two values, the second assignment causes the value 10 to be "forgotten."
Last edited on
btw were you able to get your source compiled?
All this I'm on board with because to me I'm thinking that both variable names are being assigned to the same numerical value.

Nothing is being done to variable names.


Then why does what the old address contains still appear in the output if the address was dropped?

The address wasn't 'dropped'. The address contained by mypointer changed from &firstvalue to &secondvalue. This doesn't mean that the variable firstvalue ceased to exist, and it is the variable firstvalue and the variable secondvalue who's values are output. The output statement doesn't reference the pointer at all.

Perhaps it would help to visualize what's happening:
http://s18.postimage.org/ft0s4tjeh/pointers.png
Topic archived. No new replies allowed.