Pointers

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.

Correct?

Last edited on
closed account (zb0S216C)
Your notes are correct.
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?
Dereferencing.
You'll have to be abit clearer :D
Ok. Consider:

1
2
3
int a = 2;
int* p = &a;
cout << *p << endl;

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.
To put it another way:

A pointer is, as you know, just an address. You need the type there so you know how to interpret the actual data at that address.
Are pointers just variables? If so why didnt i have to give mypointer a variable type?

Also is it possible to change the address in memory by putting &firstvalue = 1232, so then firstvalue would be stored at 1232?
Last edited on
Yes. int* is of type "pointer to an int".

No.
Ok, so do we always have to tell a pointer (which is just a variable) which type of data it is going to point to then?
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.

-Albatross
Last edited on
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?
You are always giving the pointer a type.

1
2
3
4
int* a; // type: "pointer to int"
char* a; // type: "pointer to char"
bool* a; // type: "pointer to bool"
void* a; // type: "pointer to an unknown 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).

Correct?
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

?
What do you mean by
the type for pointers is what they point to
? The type for a pointer is a "pointer to something". The type name before the * is the type it is pointing to, if that's what you mean.
That is what i mean, thanks
closed account (z05DSL3A)
Tip: read the read the declaration backwards, it is very useful especially when const in involved.
1
2
int const * a_ptr;  // a_pter is a pointer to a const int
int * const b_ptr;  // b_ptr is a const pointer to an int 

Topic archived. No new replies allowed.