kharthi 0 Report post Posted September 28, 2011 (edited) Hi, I need to implement a class cppArray which is identical to a one dimensional C++ array. It should perform range checking(index set is a set of consecutive integer from 0). It allows only one array to be assigned to another array using = operator. Supports a function that returns the size of an array. It allows reading and printing through use of cout and cin. Immediate response will help out a lot, thanks in advance! Edited September 28, 2011 by kharthi (see edit history) Share this post Link to post Share on other sites
mcddictator 0 Report post Posted September 29, 2011 Hi, I need to implement a class cppArray which is identical to a one dimensional C++ array. It should perform range checking(index set is a set of consecutive integer from 0). It allows only one array to be assigned to another array using = operator. Supports a function that returns the size of an array. It allows reading and printing through use of cout and cin. Immediate response will help out a lot, thanks in advance! Sizeof array ? string str[] = {"123","456","789"} cout<<"Array size:"<<sizeof(str)/sizeof(str[0]); output: Array size:3 Is that what you want? Share this post Link to post Share on other sites
iGuest 3 Report post Posted October 2, 2011 Yes.. Thank you. But, can you please help out in sorting the other questions I have asked. Share this post Link to post Share on other sites
multfilm 0 Report post Posted May 26, 2012 So... for the range checking, you could do something likeif (index >= sizeof(str)/sizeof(str[0])) return -1; (error)SizeOf array could be kept as a separate variable.For COUT, you could just forward it to the printf("%s", str); method.Is there anything else? Share this post Link to post Share on other sites
k_nitin_r 8 Report post Posted August 12, 2012 The sizeof operation does work for arrays in C when you use it in the same scope that the array was declared, but the moment you pass the array as a parameter, the array degrades into a pointer and you won't have any way of determining the size other than by passing in the size of the array as a parameter. I am guessing that the compiler can tell what the size of an array is, but the size is not maintained at run time so you have to define a variable to keep track of the size, if you intend to pass the array to a function at some point.In either case, you could drop the use of arrays entirely and use dynamically allocated memory because arrays are typically of a fixed size (ignoring the extensions that some compilers lend to the C language) and if you allocate too much space for an array, you're either wasting space or you will run out of stack space eventually. Share this post Link to post Share on other sites