Advertisement

the point of pointers

Started by January 17, 2000 12:05 PM
13 comments, last by webspynner_99 24 years, 11 months ago
Actually, a lot of the things you guys are mentioning can be done without pointers. Go references! Lots of good efficient languages live and work happily without pointers (Scheme, LISP, Java, VB).

For example the reference version of the first code snippet:
int x;
int &ref_x;
x = ''h'';
&ref_x = &x
ref_x = ''i'';

The first time I saw a reference call on a struct, I almost went into a heart attack. I though a guy was passing almost 32 kilobytes on the stack! This was back in 16 bit DOS, where 640K would be good enough for anybody.

Of course I would go into withdrawal if I was ever deprived of my pointers, but that''s another story.
Newsflash, SiCrane!

A C++ "reference" is nothing more than a self-dereferencing pointer.<br><br>And I wouldn''t qualify VB as "efficient" ! VB just "hides" pointers from the programmer. It still uses them. Does ByRef ring a bell? </b>
Advertisement
Functionally in C++, a reference behaves like a self-dereferencing pointer, but actually the concept is much more powerful than that. With references in C++, the context makes many of the same pointer type errors less frequent and severe. In some other languages, references are protected in some way or another.

You should really stop confusing implementation with functional symantics. It makes picking up new languages that much harder, and hurts you in the long run.

btw, have you even tried to program VB efficiently? Or even tried it at all? Or are you just parrotting?
I think it''s worth noting for webspynner_99 that SiCrane''s reference example won''t work in C, it''s C++ code (in case you tried it and it didn''t compile).

Also, foofightr is right, at first pointers are a pain, but keep at it and you''ll find yourself wondering what you ever did without them. One of the simplest uses for a pointer is in a linked list. They''re a structure that contains a pointer to a structure like itself:

struct list_struct
{
int the_data;
struct list_struct *next_in_list;
};

next_in_list has to be a pointer, if not you''d have an endless loop Using this structure you can make a long chain of list_struct''s, insert new ones, remove old ones, sort them, etc. Of course you can do the same thing with an array, but the beauty of a list is that it can grow to any size (up to available memory) at run time, and without you having to predict any kind of "maximum" size like you do with an array. Great for databases and much more. When you''re learning the basics of C, pay attention to them but they''re probably not so useful ...yet

Hope this helps


-ns-
-ns-
Wow! Thanks for all the replies, and in such a short time period (one day). I''ll keep at it and see what I get. I hope other beginners come along and see this, as apparently almost everyone has had the same problem at one time or another. If anyone comes up with any other suggestions or examples, please, post them.

This topic is closed to new replies.

Advertisement