#include <iostream>
usingnamespace std;
struct arg_struct {
double vec[100];
int length;
};
int func(void *argument)
{
struct arg_struct args;
args.vec = (double) argument;
args.length = sizeof(argument)/sizeof(argument[0]);
cout << args.length << "\n\n";
return 0;
}
int main()
{
double Q[100];
int x;
x = func(Q);
return 0;
}
i receive from the compiler the errors:
file.cpp: In function int func(void*):
file.cpp:13: error: invalid cast from type void* to type double
file.cpp:14: error: pointer of type void* used in arithmetic
file.cpp:14: error: void* is not a pointer-to-object type
#include <iostream>
usingnamespace std;
struct arg_struct {
arg_struct(){vec=newdouble[100];}
double* vec;
int length;
};
int func(double *argument)
{
struct arg_struct args;
args.vec = argument;
//the next line wont return what you expect,sizeof(argument) is 4
//while sizeof(Q) is a lot larger(800 if you check)
args.length = sizeof(argument)/sizeof(argument[0]);
cout << args.length << "\n\n";
return 0;
}
int main()
{
double Q[100];
int x;
x = func(Q);
system("pause");
return 0;
}
instead of using sizeof()/sizeof() you can use a loop,or simply compute the lenght outside the function.
i hope that helps.
file.cpp: In function int func(void*):
file.cpp:15: error: invalid conversion from void* to double*
file.cpp:16: error: pointer of type void * used in arithmetic
file.cpp:16: error: void* is not a pointer-to-object type
How to solve it please?
2)
@MasterAsh:
instead of using sizeof()/sizeof() you can use a loop
When you say loop what do you exactly mean ? The loop will start from 0 and what will be the end of the loop ?
file.cpp: In function int func(void*):
file.cpp:17: error: pointer of type void * used in arithmetic
file.cpp:17: error: void* is not a pointer-to-object type
can't a destructor prevent the memory leak?~arg_struct(){delete[](vec);}
also sorry about the system("pause") thing,i originally added that and a cout<< statement
just to check some values i had assigned to Q,but for some reason i forgot to delete that line and not the ones
surrounding it.even noobs like me wont recommend system("pause")lol,really sorry about that.
and what i meant by a loop was exactly what codekiddy said.
Thanks for the answers but i am still stuck. I do not want to change the parameters (from void to double etc). I create an array with name Q, initiallize it and print it in main function. I want to print the array in fuction funcM and funcS, but i receive errors.
#include <iostream>
usingnamespace std;
struct arg_struct {
double vec[10];
};
void *funcS(void *argS)
{
//print array in funcS.
cout << "\nfuncS Function:\n";
for(int i = 0; i < 10; ++i){
//cout << arg -> vec[i] << "\n";
}
return NULL;
}
int funcM(void *argM)
{
struct arg_struct arg;
for(int i = 0; i < 10; ++i){
arg.vec[i] = static_cast<double>(argM[i]);
}
//print array in funcM
cout << "\nfuncM Function:\n";
for(int i = 0; i < 10; ++i){
cout << arg.vec[i] << "\n";
}
funcS((void *)&arg);
return 0;
}
int main()
{
double Q[10];
int x;
//print array in main.
cout << "\nmain Function:\n";
for(int i=0;i<10;i++){
Q[i] = i+5;
cout << Q[i] << "\n";
}
x = funcM(Q);
return 0;
}
the output :
file.cpp: In function int funcM(void*):
file.cpp:25: error: pointer of type void * used in arithmetic
file.cpp:25: error: void* is not a pointer-to-object type
i am using loop in line 25 but i receive the errors. Any help please ?