Size of array passed as parameter.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Size of array passed as parameter.

 
0
  #1
Jul 5th, 2008
I need to find the number of elements in an array passed into a function. The problem is in this case, sizeof() returns the size of the pointer itself. It only does this when passed through to a function. So here is that function:
  1. bool matchWord(TWORD *wordList, TWORD *word, char *str)
  2. {
  3. for(int i = 0; i < sizeof(wordList) / sizeof(wordList[0]); i++)
  4. {
  5. if(_stricmp(wordList[i]->str, str) == 0)
  6. {
  7. word->str = wordList[i].str;
  8. word->id = wordList[i].id;
  9. word->pos = wordList[i].pos;
  10. return true;
  11. }
  12. }
  13. return false;
  14. }
Here is the array definition if you need to look at it:
  1. TWORD *wordList = new TWORD[2];
  2. wordList[0].setValues("move", WORD_MOVE, POS_VERB, -1);
  3. wordList[1].setValues("north", WORD_NORTH, POS_NOUN, -1);
And although you really shouldn't need to look at is for this problem, here is the definition of the TWORD class:
  1. class TWORD
  2. {
  3. public:
  4. char *str;
  5. int id;
  6. int pos;
  7. int index;
  8.  
  9. TWORD();
  10. TWORD(char *str, int id, int pos, int index);
  11. void setValues(char *str, int id, int pos, int index);
  12. char *posToString();
  13. char *toString();
  14. };
  15.  
  16. TWORD::TWORD()
  17. {
  18. this->str = NULL;
  19. this->id = 0;
  20. this->pos = 0;
  21. this->index = -1;
  22. }
  23.  
  24. TWORD::TWORD(char *str, int id, int pos, int index)
  25. {
  26. this->str = str;
  27. this->id = id;
  28. this->pos = pos;
  29. this->index = index;
  30. }
  31.  
  32. void TWORD::setValues(char *str, int id, int pos, int index)
  33. {
  34. this->str = str;
  35. this->id = id;
  36. this->pos = pos;
  37. this->index = index;
  38. }
  39.  
  40. char *TWORD::posToString()
  41. {
  42. switch(this->pos)
  43. {
  44. case POS_NOUN:
  45. {
  46. return "noun";
  47. } break;
  48. case POS_VERB:
  49. {
  50. return "verb";
  51. } break;
  52. }
  53. return "unknown";
  54. }
  55.  
  56. char *TWORD::toString()
  57. {
  58. char *str = new char[100];
  59. sprintf(str, "String: %s\nID: %d\nPos: %s\nIndex: %d\n", this->str, this->id, this->posToString(), this->index);
  60. return str;
  61. }
Please don't suggest the vector template, I usually stick to straight C and this is about as far away from that as I am really willing to get.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Size of array passed as parameter.

 
0
  #2
Jul 5th, 2008
Unfortunately what you are trying to do can't be done directly.

The only time the sizeof/sizeof idiom works is on the original array[] variable. Pointers and function arguments etc don't know anything about the original array...

The two usual solutions are:
  1. Pass the size of the array as an argument.
  2. Place a null value at the end of the array (just like a string ends with '\0').

Sorry there isn't a better answer...
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: Size of array passed as parameter.

 
0
  #3
Jul 5th, 2008
I kind of thought as much. Well thanks anyway.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC