»
 

Go Back   ResellerRatings Store Ratings > ResellerRatings Forums > Tech Support

Reply
 
LinkBack Thread Tools Display Modes
Old 10-13-2003, 04:29 PM   #1 (permalink)
Registered User
 
SpookyEddy's Avatar
 
Join Date: Oct 2001
Location: UK
Posts: 3,125
SpookyEddy is on a distinguished road
Pointer Kung Foo (c++)

I am learning c++, however I am used to higher level languages with no messing around with memory managment and pointers etc. I have got to grips with the basic idea of pointers, the whole "variable that contains a memory address" deal and referencing/dereferencing etc.

That said, pointers are something I never normaly use as they don't exist in most of the languages I use (Ruby, perl, vbscript etc). So.. I want you to sell me pointers. What are the cool (and preferably fairly simple) things I can do with pointers just to mess around and get a better understanding?

Thanks

ed

SpookyEddy is offline   Reply With Quote
Old 10-23-2003, 06:04 PM   #2 (permalink)
Registered User
 
Join Date: Apr 2002
Location: Georgia
Posts: 137
Jüš† ä gü¥ is on a distinguished road
bool function(){
int number;
int* numptr;
numptr=&number;
*numptr=5;
return (number==5);
}


You will always get "true"

Actually, understand that example and you'll pretty much have pointers under your belt.
__________________
Jüš† ä €öm¶ù†Ê® §ÇÌÈñŒ mÅjÒ®
Jüš† ä gü¥ is offline   Reply With Quote
Old 10-23-2003, 07:33 PM   #3 (permalink)
Registered User
 
jkrohn's Avatar
 
Join Date: Oct 2001
Location: Champaign, IL
Posts: 3,253
jkrohn is on a distinguished road
Send a message via ICQ to jkrohn Send a message via AIM to jkrohn Send a message via Yahoo to jkrohn
Pointers do exist in Perl as references though they do not allow direct memory manipulation as c++ does.

What are the uses? Plenty. Mainly I find them most useful in creation of data structures. Trees, lists, graphs, etc.

Such things are either not possible or much more difficult in other languages. (Recursive link lists anyone ?)

Jkrohn
__________________
Jkrohn
jkrohn is offline   Reply With Quote
Old 10-24-2003, 01:15 PM   #4 (permalink)
Registered User
 
Join Date: Apr 2002
Posts: 58
JohnRoboto is on a distinguished road
Java (which does not use pointers) contains trees, lists, graphs, etc.. built directly in the native API.

IMHO, pointers are excessively confusing and can be dangerous and I personally haven't found any direct advantage to using them. Perhaps someone can inform me?
JohnRoboto is offline   Reply With Quote
Old 10-24-2003, 06:02 PM   #5 (permalink)
Registered User
 
SpookyEddy's Avatar
 
Join Date: Oct 2001
Location: UK
Posts: 3,125
SpookyEddy is on a distinguished road
Quote:
Actually, understand that example and you'll pretty much have pointers under your belt.
While I understand that example fine, it seems about as simple as pointers get. What I was looking for was common uses of pointers really. Thanks anyway though.

Thanks for the ideas jkrohn, the stuff you said on irc was interesting.

Regards

ed

Last edited by SpookyEddy; 10-25-2003 at 12:38 PM.
SpookyEddy is offline   Reply With Quote
Old 10-24-2003, 06:20 PM   #6 (permalink)
Registered User
 
Join Date: Oct 2001
Location: australia
Posts: 444
KraM is on a distinguished road
I can think of a couple of uses...

If you need a function to return more than 1 value or an array:

Code:
int aFunction(int input, int *return2) {
  int *b;
  b = malloc(input * sizeof(int));
  // put something in array
  b[0] = 10;
  b[input - 1] = 200;
  *return2 = b;
  return anotherValue;
}
scanf uses pointers to put read in values into whatever memory locations its supplied arguments are pointing to.

You need it for manipulating specific memory locations. I would suppose device drivers need this facility:

*((int*)0x200000) = 0x55;

This would put 0x55 in memory location 0x200000.

This is C though, I don't know if it would work in C++.
KraM is offline   Reply With Quote
Reply




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Most Active Discussions

Recent Discussions

All times are GMT -6. The time now is 06:26 AM.