| | |
Size of array passed as parameter.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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:
Here is the array definition if you need to look at it:
And although you really shouldn't need to look at is for this problem, here is the definition of the TWORD class:
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.
c++ Syntax (Toggle Plain Text)
bool matchWord(TWORD *wordList, TWORD *word, char *str) { for(int i = 0; i < sizeof(wordList) / sizeof(wordList[0]); i++) { if(_stricmp(wordList[i]->str, str) == 0) { word->str = wordList[i].str; word->id = wordList[i].id; word->pos = wordList[i].pos; return true; } } return false; }
c++ Syntax (Toggle Plain Text)
TWORD *wordList = new TWORD[2]; wordList[0].setValues("move", WORD_MOVE, POS_VERB, -1); wordList[1].setValues("north", WORD_NORTH, POS_NOUN, -1);
c++ Syntax (Toggle Plain Text)
class TWORD { public: char *str; int id; int pos; int index; TWORD(); TWORD(char *str, int id, int pos, int index); void setValues(char *str, int id, int pos, int index); char *posToString(); char *toString(); }; TWORD::TWORD() { this->str = NULL; this->id = 0; this->pos = 0; this->index = -1; } TWORD::TWORD(char *str, int id, int pos, int index) { this->str = str; this->id = id; this->pos = pos; this->index = index; } void TWORD::setValues(char *str, int id, int pos, int index) { this->str = str; this->id = id; this->pos = pos; this->index = index; } char *TWORD::posToString() { switch(this->pos) { case POS_NOUN: { return "noun"; } break; case POS_VERB: { return "verb"; } break; } return "unknown"; } char *TWORD::toString() { char *str = new char[100]; sprintf(str, "String: %s\nID: %d\nPos: %s\nIndex: %d\n", this->str, this->id, this->posToString(), this->index); return str; }
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:
Sorry there isn't a better answer...
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:
- Pass the size of the array as an argument.
- Place a null value at the end of the array (just like a string ends with '\0').
Sorry there isn't a better answer...
![]() |
Similar Threads
- get length of a dynamic array (C++)
- Function syntax to take in a char array and return keyboard input (C)
- Please Help Dynamic Memory Allocation. (C++)
- 2-D array (C++)
- template issues - need expert debugger! (C++)
- This ought to be simple - extra spaces (PHP)
- priority queue (C++)
- Please help with my quicksort (C)
- How do I create a program using an Array ? (C++)
Other Threads in the C++ Forum
- Previous Thread: HELP With Counter-Strike Bot!
- Next Thread: can i get the full file path from specified file name (.exe)
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






