[cs23021-2] Lab 10 clarification

Mikhail Nesterenko mikhail at cs.kent.edu
Sun Mar 28 19:32:49 EDT 2010


Mikhail Nesterenko wrote:
> CSI students,
> 
> In the 10-th lab, in the header file the output() function was
> prototyped as follows:
> 
>    // prints the values in "array" of "size" in sorted order
>    void output(int *const array, int size);
> 
> That is, the pointer to the array was passed as a constant (not to be
> modified). However, if, to print the numbers in the increasing order,
> you try to use removeNumber() function, the compiler will allow

will NOT allow it

> it. The reason is: the remove number is prototyped as follows
> 
>    // removes a "number" from the "array" of "size".
>    // if "number" not there -- no action
>    // note, "size" changes
>    void removeNumber(int *& array, int number, int &size);
> 
> Note that the array is passed by reference and can be modified. That
> is "array" pointer is not constant. If you try to invoke
> removeNumber() from output(), the compiler would generate an error
> stating that there is an attempt to convert from constant pointer type
> to regular pointer type.
> 
> In the header file I updated the output() prototype to non-constant
> type:
> 
>    // prints the values in "array" of "size" in sorted order
>    void output(int *array, int size);
> 
> In your implementation, you are free to use either the old (constant)
> or the new (non-constant) pointer to the array.


More information about the cs23021-2 mailing list