• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array length Compared to C++  RSS feed

 
Down Shi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In java the size of an array can be represented by Array.length, but I am just curious to know if any one knows how the C++ represents the size of an array ? Although it's a forum for Java Certificate purpose, I hope some interested to answer my question.
Thanks
 
Ali Fakhari
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Down
It depends on the way that you have declared the array: statically or dynamically. So please consider the following cases:
/* First case; statically created arrays */
char /* or any other type */ a[ANY_CONSTANT_INTEGER_VALUE];
...
int nArraySize = sizeof(a) / sizeof(a[0]);
Or you could initialize the nArraySize by the constant integer value that you used for defining the array.
/* Second case; the dynamically created arrays */
char *p = new char[any_integer_expression];
any_integer_expression means a constant, or a variable or any composition of them even function calls.
In this case you know the array size in advance.
/* Third case; you have an array created by another part
of the program */
void Sample(char a[])
{
...
}
In this case, there is not any way to find size of the array !!
C++ is not as programmer friendly as Java !
Is it?

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!