understanding the use of pointers.....

Started by
26 comments, last by werdy666 21 years, 10 months ago
Hi, I am trying to teach myself pointers. At the moment i understand that after declaring a pointer such as "int *ptr" ptr = the address of where the pointer is pointing to. *ptr = the variable in that address. &ptr = the address of the pointer. after a little bit of experimentation i came up with this...


void function();

int *ptr;

int main()
{

int value1 = 100;
ptr = &value1

cout << value1 << endl;
cout << *ptr << endl;

function();

cout << value1 << endl;
cout << *ptr << endl;

return 0;
}

void function()
{
int value2 = 200;
*ptr = value2;
}

 
What i could do now is return a variable from my function even though its a void function. so if it was an int instead of a void, i could return 2 variables. I would like to know is where would u use pointers like this? are pointers better than just using global variables? And can anyone show me another example like my basic program here, that can demonstrate a use of pointers in a program? I'm finally understanding what they are, and now i just need to work out where i can use them, when i should use them & why people say they are very useful!:D Thanks for your time! :D Werdy666:D [edited by - werdy666 on May 22, 2002 11:46:32 AM]
Advertisement
The usage you have shown is no different from global variables, and certainly isn't the same as returning a value from a function. All you are doing is setting a global variable (via a pointer) to a value, all within the body of a function. You also have a problem. When the function returns, value2 will no longer exist, so "ptr" will point at invalid storage.

quote:Original post by werdy666
I'm finally understanding what they are, and now i just need to work out where i can use them, when i should use them & why people say they are very useful!

People say a lot of things, and people often speak from ignorance. The rules[1] for using pointers in C++ are to only use them when there is no convenient alternative. In most contexts, a convenient and superior alternative is available in the form of smart pointers. The book Accelerated C++ will teach you how to use C++ as it is intended by its creators.

[1] of course, they're not really rules, but heuristic guidelines distilled from many years of collective experience, also known as "patterns".

[ C++ FAQ Lite | ACCU | Boost | Python | Agile Manifesto! ]

[edited by - SabreMan on May 22, 2002 11:55:28 AM]
BTW, SabreMan''s a little off on one small point: when the function returns in your code ptr will point to a valid address, as it was previously set to point to value1 in main. Still, this is a poor use of pointers, as it would have been much easier to simply have the function return an int.
quote:Original post by Melraidin
BTW, SabreMan''s a little off on one small point

Yes, sorry. For some reason I read it as "ptr = &value2". Brain fart!

[ C++ FAQ Lite | ACCU | Boost | Python | Agile Manifesto! ]
Especially considering a pointer is 32 bits long and so is an int.

Also, a quick lesson on NULL pointers (void*):

Main(argcnt, arg[])
{
long* ptr; //32bit address that points to nothing
long var; //long var

ptr = &var; //Ptr points to memory space of var

ptr = (long*)ReturnFunction(); //now points to memory space
/////////////////////////////////of the return value of
/////////////////////////////////ReturnFunction and it''s of
/////////////////////////////////length ''long''
}
void* ReturnFunction()
{
static long number = 0; //static for longer life
return (void*)&number;
}

This allows you to create a generic system that passes pointers of NULL type that is determined what they are by the function that recieves them

Struct Message_Struct{
DWORD Message;
void* lparam;
void* wparam;
}

It's perfectly reasonable to use pointers to return 'extra' values from functions, but as pointed out above, a more conventional syntax would have the pointer as an argument to the function:


    void function(int *ptr);int main(){  int value1 = 100;  int *ptr = &value1  cout << value1 << endl;  cout << *ptr << endl;  function(ptr);  cout << value1 << endl;cout << *ptr << endl;  return 0;}void function(int *ptr){  int value2 = 200;  *ptr = value2;}    


It generally comes down to experience and personal preference as to whether you use pointers, smart pointers or whatever. You see this type of thing a lot in C, C++ has other ways which are not necessarily as straightforward but can provide some benefits in large programs.

EDIT: decls

[edited by - JuNC on May 22, 2002 1:13:15 PM]
To answer your qeustion: Why pointers are useful?
in C: passing arguments by reference

in C++: polymorphism through inheritance

in C/C++: dynamic allocation (using heap-memory)

I''m sure the above list is lacking - but they''re the more obvious benefits.

IMO, the beauty of a pointer is - I believe it''s called - indirection or aliasing. You can modify/access "objects" indirectly. This can be dangerous in certain contexts, but then again, it all comes back to experience: knowing where/how to use them.
People fear what they don''t understand, hate what they can''t conquer!
OK, I''m finishing my first semester of high school C++, and I''m making a hotseat game where two people duel. Each person has a number of statistics, and their actions go through formulae. So I have the structures Person1 and Person2

Instead of making functions for Person1''s damage on Person2, etc, and vice versa, I want to have a pointer for the CurrentPlayer and another pointer for CurrentEnemy, and just run my formulae through those generalizations, and after each turn switch the two.

Is this good?
quote:Original post by Anonymous Poster
Especially considering a pointer is 32 bits long and so is an int.

Not necessarily.
quote:Original post by JuNC
a more conventional syntax would have the pointer as an argument to the function:

Reference. A more conventional syntax would use a reference.
quote:Original post by Bashar
in C++: polymorphism through inheritance

Do you see a pointer in the following?

    #include <iostream>class base{public:	virtual void f() const 	{ std::cout << "base::f()" << std::endl; }};class derived : public base{public:	void f() const 	{ std::cout << "derived::f()" << std::endl; }};void func(const base& b){	b.f();}int main(){	base b;	derived d;	func(b);	func(d);}    


quote:
in C/C++: dynamic allocation (using heap-memory)

Many aims of which can be satisfied using a standard container.

[ C++ FAQ Lite | ACCU | Boost | Python | Agile Manifesto! ]

[edited by - SabreMan on May 23, 2002 5:27:19 AM]
quote:Original post by bartkusa
Instead of making functions for Person1's damage on Person2, etc, and vice versa, I want to have a pointer for the CurrentPlayer and another pointer for CurrentEnemy, and just run my formulae through those generalizations, and after each turn switch the two.

Is this good?

Possibly not. You haven't really explained enough to be able to tell, but this sounds like a potential application of the visitor pattern.

[ C++ FAQ Lite | ACCU | Boost | Python | Agile Manifesto! ]

[edited by - SabreMan on May 23, 2002 5:29:06 AM]

This topic is closed to new replies.

Advertisement