error casting pointers

B

Baloff

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 = val;
cout << ia << "\n";
}
}


int main(){
int a[5] = {};

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

Larry I Smith

Baloff said:
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 said:
for(int i=0; i<byte; ++i){
ia = val;
cout << ia << "\n";
}
}


int main(){
int a[5] = {};

//void* vp = static_cast<void*> a;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Problem with codewars. 5
casting 1
Help with pointers 1
Lexical Analysis on C++ 1
Compiler error using g++ from MinGW 1
Templates and g++ 4
undefined reference(compile error), plz help 4
using my template class 11

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top