Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

zippo

Array of Pointers and a Pointer of an Array

This topic is 6929 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My goal is to become a game programmer, but first I require a solid foundation on programming itself. I have a history of QBASIC, C, and a few other, fairly outdated languages (thanks a lot high school ). Anyways, I''m now settling down with C++. I realize this is a powerful langauge, one which I feel is best to use in game programming. I''ve gotten fairly far, as I already understand many of the basic concepts (looping, encapsulation, arrays, variables, etc.). However, pointers and references did set me back a wee bit, but nothing I couldn''t handle with a little re-reading. :-) Well, I''m now getting into pointers and arrays, and I''ve read about an array of pointers, and a pointer of an array, and understand how they differ. Where my question arrises, though, is when to use which. I''ve seen examples of both operations performing the same functions. Is this a matter of personal preference, or are there defined areas where I should use one over the other? All replies are welcomed. Thanks! Z¡PPÕ

Share this post


Link to post
Share on other sites
Advertisement
I wouldn''t worry about it too much right now. With me, and everyone else I''ve heard about on the board, pointers were troublesome at first. However, probably, after working for a while with the language, and just practicing, one day you''ll just realize that it''s clicked, and you''ll understand it.

Pointers are weird, and different from conventional variables in how you must think about them, but eventually you''ll just realize that you can''t live without them

Share this post


Link to post
Share on other sites
Well there are many situations where you would use either.. but here is a little generalization..

Ok well I sat down to write a little generalization and realized that it''s much harder than I thought at first.

How can I write this and sound like I know what I''m talking about and not be too confusing at the same time?

I assume you are talking about an array of pointers and a pointer to an array. In many cases, these are almost the same thing.

If you have this:
char *ArrayOfPtr[10];
then that is an array of ten pointers.. but ArrayOfPtr by itself is a pointer to that array.

You would definatly use an array of pointers if you wanted an array of arrays (including an array of strings) and especially if the individual arrays were of different sizes.

You would use an array of pointers if you were going to be moving elements of the array around, or copying them. If your array is holding more than simple data, it is much faster to sort an array of pointers to data than to sort an array with the actual data.

The other thing you were talking about.. a pointer of array.. I take to mean a pointer to an array.. You generally wouldn''t use one of these directly.. except maybe in a function declaration.

So basically if you have an array, and one or more of the following is true, use an array of pointers:

- The elements of the array are large and will be moved around, sorted, allocated individually or on-demand..
- The elements are of different sizes (ie.. an array of pointers that point to strings is a good example. Each string can be as long or as short as it wants. If you used a 2 dimensional array it would give each string a fixed length. Strings shorter than the fixed length would waste space)
- The elements are of different types. If you are making an array of classes and those classes have any virtual functions, you would do best to use an array of pointers so that the proper derived function would be called for that class. This can be a very powerful technique..

Reasons not to use an array of pointers:
- Each element has to be allocated individually and deleted individually, usually there is a small overhead for this, both in time and memory usage.
- Dereferencing the pointer is a tad slower than directly indexing an array of data
- Having all the data in sequence can fit better in the CPU cache.. speeding access to the data.


After writing this I feel like I wasn''t much help.. :/
But if you have a more specific question I''d be happy to try and answer it..

Good luck,
Adam M.

adamm@san.rr.com

Share this post


Link to post
Share on other sites
Thanks for all the advice. I wasn''t really looking for anything in particular for an answer, just a general look at when and when not to use the two. I was referring to a pointer to an array, simply poor grammar on my part.
You''ve answered my question well, and I feel I can go ahead and move on. Thanks!

Z¡PPÕ

Share this post


Link to post
Share on other sites
I''m just curious: Why didn''t they teach you pointers when you were learning C? I can''t imagine getting by in C without them.

Share this post


Link to post
Share on other sites
It''s not that they didn''t teach pointers, it''s that it was the first class in the morning, so I slept while someone else gave me their code.

Not the best of things to do if I''m looking to get into programming, but at the time I wasn''t that interested.

Z¡PPÕ

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!