Jump to content
  • Advertisement

Archived

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

Xtreme

pointer to an array of pointers **

This topic is 6306 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

ok here is a tough question! We all know what pointers are right? Well if I''m correct, a pointer to an array of pointers holds nothing but the starting address of a pointer. So if I have this: #DEFINE FILE1 "file1.ext" #DEFINE FILE2 "file2.ext" and group them like this: char *files[] = {FILE1, FILE2}; how can I determine how many elements there are in the array without having to count them myself?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Try this:

#define NumElements(x) (sizeof(x) / sizeof(x[0]))

Then you can:

char *files[] = { "file1", "file2", "file3" };

numfiles = NumElements(files);

Obviously the macro is not needed, but it can be handy.

BTW, In the above case, the sizeof doesn''t actually include the size of the characters in the strings, just the size array of pointers to char (and thats why this works).

The double-quoted strings make it as if you did:

char hiddenstr1[] = { ''f'',''i'',''l'',''e'',''1'' };
char hiddenstr2[] = { ''f'',''i'',''l'',''e'',''2'' };
char hiddenstr3[] = { ''f'',''i'',''l'',''e'',''3'' };

char *files[] = {&hiddenstr1[0], &hiddenstr2[0], &hiddenstr3[0]);

So, sizeof(files) wouldn''t include storage used for the actual characters, just the three pointers.

Share this post


Link to post
Share on other sites
thanks Anon!
so precisley how does (sizeof(x) / sizeof(x[0])) work?
What I mean here is isn''t sizeof(x) = 4 bytes since it is a pointer?

Share this post


Link to post
Share on other sites
absolutely not, sizeof(x) is compiling-time-variable, it''s not sizeof(void*)

------------------------------
Dedicate to nobody, I''m nobody

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!