By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,915 Members | 1,280 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,915 IT Pros & Developers. It's quick & easy.

error casting pointers

P: n/a

Hello
could somebody please tell me what is wrong with my solution to the
following exercise?

many thanks

Define an array of int. Take the starting address of that array and
use static_cast to convert it into an void*. Write a function that
takes a void*, a number (indicating a number of bytes), and a value
(indicating the value to which each byte should be set) as arguments.
The function should set each byte in the specified range to the
specified value. Try out the function on your array of int.

I am not sure why I am getting this error

******************************error*************** ***************
main.cpp: In function `void ini(void*, int, int)':
main.cpp:5: error: parse error before `;' token
main.cpp: In function `int main()':
main.cpp:16: error: parse error before `;' token
make: *** [main.o] Error 1
make: Target `proj1' not remade because of errors.
******************************code**************** **************
#include <iostream>
using namespace std;

void ini(void* vp, int byte, int val){
int* ia = static_cast<int*> vp;
for(int i=0; i<byte; ++i){
ia[i] = val;
cout << ia[i] << "\n";
}
}
int main(){
int a[5] = {};

void* vp = static_cast<void*> a;
ini(vp, 5, 1);
}

Aug 4 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Baloff wrote:
Hello
could somebody please tell me what is wrong with my solution to the
following exercise?

many thanks

Define an array of int. Take the starting address of that array and
use static_cast to convert it into an void*. Write a function that
takes a void*, a number (indicating a number of bytes), and a value
(indicating the value to which each byte should be set) as arguments.
The function should set each byte in the specified range to the
specified value. Try out the function on your array of int.

I am not sure why I am getting this error

******************************error*************** ***************
main.cpp: In function `void ini(void*, int, int)':
main.cpp:5: error: parse error before `;' token
main.cpp: In function `int main()':
main.cpp:16: error: parse error before `;' token
make: *** [main.o] Error 1
make: Target `proj1' not remade because of errors.
******************************code**************** **************
#include <iostream>
using namespace std;

void ini(void* vp, int byte, int val){
//int* ia = static_cast<int*> vp;
int* ia = static_cast<int*>(vp);
for(int i=0; i<byte; ++i){
ia[i] = val;
cout << ia[i] << "\n";
}
}
int main(){
int a[5] = {};

//void* vp = static_cast<void*> a;
void* vp = static_cast<void*>(a);
ini(vp, 5, 1);
}

Aug 4 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.