I was wondering if there was some function/operator called size which determined the size of an array in c or c++
Printable View
I was wondering if there was some function/operator called size which determined the size of an array in c or c++
No.
But there is one called 'sizeof()'. : )
If you have
You get the answer 6 by doingCode:int a[] = { 1, 0, 0, 2, 11, 99 };
sizeof(a) / sizeof(a[0] )
This only works on real arrays.
Pointers to arrays (either by function call parameter or allocated memory) do not work in this way.
is there any way of determining till what point in the array actually explicit insertion of elements has been made. i mean for example i have an array a[20]. now i ask user to input numbers into this array. if i dont keep track of the number of elements he has entered at this point is there any way of determining this at a later point of time.
Not unless you reserve some magic value (which the user cannot enter) and fill the array with that value before getting user input.
It's much easier just to keep a count of these things
Not from the a language perspective. However if you initalized the array with known values that can not be inputted then you can step through it yourself and count how many aren't of this value.
what do you mean by
"
This only works on real arrays.
Pointers to arrays (either by function call parameter or allocated memory) do not work in this way."
gooddevil can you not use a linked list, you can keep count of the number of entries made by the user
@studentc
Example:
sizeof(a) would give 2 while sizeof(b) would give 4.Code:char a[2];
char *b = a;
Example 2:
Here sizeof(a) would be 50 and sizeof(b) would be 4. See what we mean now?Code:char a[5][10]; /*A two dimensional array */
char (*b)[10] = a; /* A pointer to an array of 10 characters */