I am now trying understand pointers properly. Can you tell me if im right with my notes below:
See below:
// my first pointer
#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;
}
Notes:
By putting int * mypointer; i am making a pointer called mypointer and i am telling it that it will point to an int variable.
Then by putting mypointer = &firstvalue; i am referencing firstvalue's address in memory.
Then by putting *mypointer = 10; i am making firstvalue equal to 10 because by putting *mypointer = 10 i assign 10 not to the address of what it references, but to the actual value of what it references because i used the asterisk sign.
Good, what is the purpose of telling the mypointer variable that it is going to store an int value, why not just go ahead and put an int in there without telling it?
The type of *p has to be known for operator<< to deal with it, otherwise dereferencing p would be meaningless. There is a type of pointer that doesn't know what it points to: the void pointer. Consider:
1 2 3
int a = 2;
void* p = &a;
cout << *p << endl; // this will not compile
In this case, what would be the meaning of *p? The compiler doesn't know what kind of objects p points to, so it can't dereference it.
Well... no, but it could be dangerous to use a pointer without a type (a void pointer) if you don't know what you're doing, and there are several limitations, the most prominent one I've listed below.
Although void* pointer is very flexible in pointing to stuff, as far as I know you can't directly dereference it (I hope you can see why). You have to cast it to another type of pointer first.
If we dont always have to tell a pointer which type of data it is going to point to then how do we give the pointer a type, because after all, a pointer is just a variable isnt it, and all variables must have a type?
Ohhhhhh. So by putting int* a we are actually giving the pointer called "a" a type, which is int.
So we do ALWAYS have to tell the pointer which type it is, and if we dont want to give it a type, we give it the void type (which is still kinda a type).
Yes, basically. Except you don't really have a pointer called that has a type, a pointer to an int IS a type, different from a pointer to a char, or a void pointer.
I think you misunderstand. "int*" is itself a type. "a" is not an "int", it is a "int*" or "pointer to int".
I'm not quite sure about your wording here. I think you understand the concept but the way you are phrasing it is a little strange. Yes, you can either give the pointer an explicit type or make it point to anything using "void*". In that case, however, you cannot access the data at that memory location since you haven't specified how you want to read it.
Ah, so types for variables and types for pointers are abit different, the difference is that for variables the type would be, well, the type, and the type for pointers is what they point to