I need some help in clarifying some points about pointers. In the example below is a short program, and a description of what each line does. Am I understanding this correctly?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include <iostream>
using namespace std;
int main ()
{
int octo = 8;
cout << &octo << endl;
int *octo_pointer;
octo_pointer = &octo;
cout << octo_pointer << endl;
cout << *octo_pointer << endl;
return 0;
}
|
Line 7 - Create an int variable called octo and assign the value of 8.
Line 8 - Print out the memory address of where the variable octo is stored at.
Line 10 - Create a special int variable, a pointer, called octo_pointer.
Line 11 - Assign the pointer the memory address of the variable octo.
Line 12 - Print out the memory address of the variable the pointer is pointing to. The result should be the same as Line 8.
Line 13 - Print out the value that is stored in the address where the pointer is pointing to.
I know for very large programs using pointers saves time/space/other things, but what about for small or medium-sized programs? At what point in a program do you think, okay, this program is getting pretty big, time to start using pointers?
Now anytime I need to pass the value of a variable between functions I just use the dereference operator (int ¤t_health) or whatever, and it works fine.
It is feasible to go 100% dereference and 0% pointers? Or how feasible is it?
Thanks
Also, is the Preview glitched. Never seems to work for me.