A little help please

From: Rich (Someone_at_somewhere.com)
Date: 12/27/04


Date: Mon, 27 Dec 2004 15:35:00 +0000 (UTC)

Hello all,

I am struggling a bit with this problem.
 From my understanding of void* it is something that should generally be
avoided if possible.

What I am trying to do is
Define an array of int
Take the atart address of the array
use a static_cast to convert it to a void*

Then I have to write a function that takes as arguments
a void*
a number indicating a number of bytes
a value use to set each byte to that value.

The function should set each byte in the specified range to the value.

writing the function is not a problem afaics here is what I have done
for that

void func1( void* arr, int nBytes, int value )
{
        for( int i = 0; i < nBytes; i++ )
        {
                arr = value; // set first element to value
                arr += nBytes // increment by nBytes to get next element
        }
}

and the main function

int main()
{
        int arrX[ 10 ];
        char ch ='';
        int* i = &arrX[0]; // set i as pointer to first element of array
        void * vp = static_cast<void*> (i); // convert i to a void*
   // assign to vp
        func1( vp, sizeof( arrX ), 8 );
        for( int i = 0; i < 10; i++ )
                cout << arrX[ i ] << endl; // print out array elements
        cin >> ch; // used to pause console to see what is going on from
// VS 2003
        return 0;

} ///:~

I get a few errors compiling,
void* unknown size
void* illegal with all types.

My first question is are these errors to do with my compiler settings or
have I made a goof in the code?

My second question, what can I do to make this work correctly,
unfortunately I have to use void* so there is no getting away from this.

My third question, is am I correct in thinking that the only time to use
a void* is when the variable being assigned could be of any type?

TIA
Rich



Relevant Pages