Help with & and *

Pages: 12
1
2
3
4
5
6
7
8
9
10
11
12
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; 
} 


Is this storing the value of 10 and 20 to the locations of firstvalue and secondvalue respectively? Or of firstvalue and secondvalue being defined as 10 and 20?
I ask this because earlier i read that you should read from right to left always when programming?
thanks in advanced im so confused
When you run your program, what is the output. That should be a big clue to answer your question.



You should always initialise your variables before you use them otherwise they might be garbage.

This is one of the major reasons that coders get bugs in their code

1
2
int firstvalue = 0;
int secondvalue = 0; 


And it's good practice to comment, each variable when you declare & initialise, for what the variable is.
size of output is 577kb. how does that answer my question?
In line 6, the value 10 is being assigned in whatever mypointer is pointing at, which is firstvalue.

Thereafter, firstvalue is equal to 10.
I don't understand how your output is 577kb.

You have no loops, so it should either print the 2 lines you want or nothing.

did you initialise the variables, like I said?

What does a small section of output look like?
I modified cout a bit
1
2
cout << "firstvalue is " << &firstvalue << "  " <<firstvalue << endl; 
  cout << "secondvalue is " << &secondvalue << "  "<< secondvalue <<endl;

response was 0x28ff44 (10)
second : 0x28ff40 (20)
so is 0x28ff40 and 0x28ff44 the memory adresses of where 10 and 20 are being stored?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

int main () 
{ 
  int firstvalue=0; 
  int secondvalue=0; 
int * mypointer; 
  mypointer = &firstvalue; 
  *mypointer = 10; 
  mypointer = &secondvalue; 
  *mypointer = 20; 
  cout << "firstvalue is "  <<firstvalue << endl; 
  cout << "secondvalue is " << secondvalue <<endl; 
 system ("pause");
  return 0; 
}

yes i did this is what it looks like now. it is only printing these two lines...so why is the output 577kb?
Yes they are, because you used the & , which is "the address of" operator.

So did the original version work when you initialised the varables?
it worked even without intialising the variables
i just didnt understand what it did. also how can you choose the memory locations of first and secondvalue?
how can you choose the memory locations of first and secondvalue?


You can't.*

*You can but it's quite complex and I doubt you'll understand it at your level.
You can't choose memory locations. Why would you want to do that?

You can however do pointer arithmetic, if you are careful.

it worked even without intialising the variables


You should always initialise your variables before you use them otherwise they might be garbage.


And it's good practice to comment, each variable when you declare & initialise, to describe what the variable is for .
Idk im kinda interested in viruses already. not really to do any damage just to kinda study.
i thought basically what viruses do are replicate and alter different key memory address values?
i know thats far out there considering im still asking questions about extremely basic concepts right now though.
the variables arent for anything. this was just an example i was looking at in the manual. maybe i misunderstood and you already understood this code was meaningless though. could you give me an example on what you mean using this code?
I fooled around with putting stuff into BIOS on an old DOS machine years ago, but that was using assembly language which is way, way too hard for a novice programmer.

The things we did back then don't work any more, because people have wised up to that sort of blatant attack.

We had our problems, having to replace a BIOS chip to get the machine to go again.
is it hard to create, study, and destroy viruses that you didnt create using c++?
It's all out of my league really.

If you want to study viruses , do it under the guidance of a University Lecturer. If I was a Lecturer, I wouldn't let anyone study viruses until they had a masters degree.

You probably need to learn how to program first.
Why would you want to do that?


Self modifying code :)
How can i actually learn how to program. ive learned a lot of stuff http://www.cplusplus.com/files/tutorial.pdf
there. but i feel like im not really learning how to apply different techniques at the same time. maybe im not far enough into the tutorial yet. Im just at pointers.
Just practice. Find some interesting sounding problems, and work out how to solve them programmatically (real word?).

projecteuler.com has some pretty fun math related programs to do. They can be really challenging and require either strong math ability, or ability to look up how to solve problems you've probably never thought of before.
Pages: 12