Pointers

Started by
8 comments, last by elis-cool 22 years, 3 months ago
The book im reading could be better at explaining pointers and their use so could some people help?. So if you had: int* var = &5; cout << var; You would say intiger that points to var is at the address of 5??? And would it show the hex value of the address (var)in memory? So * means what exactly? (not multiply) And & means the address of? So what does ''pointer'' acctually mean? I really need the help with pointers as Im finding them a bit hard along with the hexadecimal system.
[email=esheppard@gmail.com]esheppard@gmail.com[/email]
Advertisement
take this ''analogy'' someone gave me.

a fly flies. a pointer points.

lets say you have

int i = 5 ;
int* ptr ;

ptr = &i


thus

ptr now holds the ADDRESS of i.
now to display the address, you simply do this:

cout << &ptr ; // which will display the value of the pointer

now if you want to change the value of i..you can do this:

*ptr = 100

hope this isnt too confusing!

do NOT set the memory location of the pointer manually! leave it alone! it will be automatically set when you point to something.

just remember...a pointer just points to memory.

int i = 100

i gets stored somewhere in memory...and when you ''point'' to that ,like in the example above, you find WHERE its located in memory! and from there you can do a lot of cool stuff...ull learn that later on though if you have questions, im me at "damn messycan" or y!"damn_messycan"

I think that what you are doing is itself confusing.

First of all, when you assign a type to a variable you attach a * to the variablename when you want a pointer. The pointer is a variable but holds only the address of a memory area that stores a type.

int *var; //holds a memory location not yet assigned
EDIT: I must be drunk or something

int var=5; //var stores the integer value 5
int *pointer;
pointer = &var //A pointer stores an address right? And var stores an integer right? To get the address of var you use a &.

If you have a pointer (an address to a memory location) and you want the actual value at that memory location you dereference it like so:
cout << *pointer; //this prints the value
cout << pointer; //This prints the memory location
cout << var; //this prints the value
cout << &var //This prints the memory location

And I would be surprised if you could do int *var=&5; because here, 5 is a constant, but I might be wrong.

By the way, some advice: when you declare a pointer, put the * sign next to the variable name and not the type. Why, it doesn´t matter? Well, declare two pointers like this:

int* var1, var2;

Are they both pointers? - No´.
It is just easier to see that var1 is a pointer and var2 not like so:
int *var1, var2;

Good luck;

Why make it simple when you can make it sooo nice and comlicated.

Edited by - clabinsky on January 11, 2002 7:34:29 PM
Why make it simple when you can make it sooo nice and complicated?
quote:
cout << &ptr ; // which will display the value of the pointer


Last time I checked this would display the address of ptr and actually the same as only using ptr

Declaring a pointer use: *
Dereferencing pointer use: * //getting the value
Getting the address of a var use: &

Remember the pointer holds the adress. You dereference it with * to get the value.

Why make it simple when you can make it sooo nice and comlicated.

Edited by - clabinsky on January 11, 2002 7:36:13 PM
Why make it simple when you can make it sooo nice and complicated?
Thanks its been a bit of help...
So is declaring it a pointer ie. int *var; a different variable type to int,char,double,long?

And what is all this "cool stuff" you can do? so far all i can see it good for is overwriting a variable in memory, but why would anyone even want to do this instead of just changing the value of the variable eg
char letter = ''n'';
to
char letter = ''p'';
why do it with pointers?

And maybe you could use it to possibly crash other programs running by you overwriting a int with a double to destroy the memory around it(if its got anything stored there).
[email=esheppard@gmail.com]esheppard@gmail.com[/email]
Sigh....

Well, first of all you have to declare the type of the pointer because you are telling the pointer that it will be pointed to a certain type - different types = different amount of memory needed and operations are done differently.

Basically, the cool thing is that you can change to where a pointer points without losing the data and when you are dealing with big structures you (usually) don´t want to send copies of the structure when calling a function for example.
    struct mystruct{    char *szName;    int iAge;    float fHeight;    ...};void somefunction1(mystruct a, bool bFlag);void somefunction2(mystruct* a, bool bFlag);void somefunction2(const mystruct* a, bool bFlag);int main(){    mystruct Obj1;    mystruct.szName="clabinsky";    int iAge=30; //Oh my god, time passes fast ;)    float fHeight=5.32f    ...        //This call copies the whole struct when the function is    //called. Any changes you make will go out of scope when you     //return.    somefunction1(Obj1,false);    //This call however only calls the function with the address    //of the struct allowing changes to be made permanently and    //a pointer is 4 bytes in size as opposed to the struct...    somefunction2(&Obj1,false);    //if you want to send the address of the struct but not     //allow any changes you should use the keyword const like so    somefunction3(&Obj1,false);         return 0}    


I am actually using C here where C++ is easier because it allows you to declare the functions with a & instead of the * and when you call the function you don´t have to specify the &Obj1 only Obj1.

Trust me, pointers are cool.

EDIT: I forgot something:
int *ptrtoaninteger;
Read it out loud ptrtoaninteger is a pointer to a memory location containing an integer. The pointer contains the address (memory location) to an integer (if initialized). If I dereference the pointer using a * I will get the value of the integer just like using a normal variable.

Why make it simple when you can make it sooo nice and complicated.

Edited by - clabinsky on January 11, 2002 8:09:32 PM
Why make it simple when you can make it sooo nice and complicated?
Thanks for the help so far guys but the one thing i''m stuck on now is how exactly do you read pointers as; eg:

int *var = 5; // but what would this be read as?
int ptr = &var // would be read as int ptr = the address of var.
*ptr = 8; // and what would this be read as?

Also what exactly is the difference between:
*num = 3; and this: num = 3; while num is a pointer?

Sorry for all the qestions but im just not getting it. How many others here had trouble with pointers? I just dont know why you would use a pointer instead of changing the variable normally. Exept when passing vaiables to a function.
[email=esheppard@gmail.com]esheppard@gmail.com[/email]
quote:Original post by elis-cool
Thanks for the help so far guys but the one thing i''m stuck on now is how exactly do you read pointers as; eg:

int *var = 5; // but what would this be read as?
int ptr = &var // would be read as int ptr = the address of var.
*ptr = 8; // and what would this be read as?

Also what exactly is the difference between:
*num = 3; and this: num = 3; while num is a pointer?

Sorry for all the qestions but im just not getting it. How many others here had trouble with pointers? I just dont know why you would use a pointer instead of changing the variable normally. Exept when passing vaiables to a function.

OK... I think you have confused yourself.

All the familiar types like char, int, float etc. contain data.
The following trivial examples all show some data being stored.

int i = 5;
int j = 4;
float f = 5.6;
char c = ''H'';

Pointers do not store data. Instead, they store the memory address of something which does store data.

int* pi = &i // pi points to i;

Now if you change the value of the pointer itself, you are changing the area in memory it is pointing at, which may not be what you want to do. In order to change the data it is pointing at, you need to dereference it....

eg...

*pi = 10 // this changes the value of i to 10.
pi = j // this changes the pointer to point at memory location 0x4 (because the value currently stored in j is 4) which is probably not what you wanted to do. Most compilers would warn you about this particular error.
pi = &j // Now pi actually points at j.
*pi = 10 // now j has a value of 10 as well.

Hopefully that will clear some things up for you. Either that or it will confuse you more, I am not sure.



thanks that helped. Finally someone who can explain it without it sounding too complicated
[email=esheppard@gmail.com]esheppard@gmail.com[/email]

This topic is closed to new replies.

Advertisement