casting problem

Hi, i have the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>	

using namespace 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


any idea please how to properly cast?
Last edited on
args.vec = (double) argument;
you can't copy array members just like that, but using for loop may solve this:

1
2
for(int i = 0; i < 100; ++i)
        args.vec[i] = static_cast<double>(argument[i]);
the aim of ur cast isn't really clear,but assuming that u want to make args_vec an exact copy of Q,
i'd suggest you make these changes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>	

using namespace std;

struct arg_struct {
    arg_struct(){vec=new double[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.

1)

i changed the line 25, the code is :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>	

using namespace std;

struct arg_struct {
        //double vec[100];
	arg_struct(){vec=new double[100];}
        double* vec;
	int length;
};

int func(void *argument)
{
	struct arg_struct args;
	args.vec = argument;
        args.length = sizeof(argument)/sizeof(argument[0]);
	cout << args.length << "\n\n";
        return 0;
}

int main()
{
	double Q[100];
	int x;
	x = func((void *)Q);
	//system("pause");
        return 0;
}


but i receive the same errors like before:

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 ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
struct arg_struct {
    double vec[100];
	int length;
};
int func(void *argument) {
	struct arg_struct args;
	args.length = -1;
	while(++args.length < 100)
		args.vec[args.length] = static_cast<double*>(argument)[args.length];
	return args.length;
}
int main() {
    double Q[100];
    int x = func(Q);
    cout << "lenght of array is: " << x;
    cin.ignore();
    return 0;
}
Last edited on
Thanks for the help.

The last question is that i have changed line 25 from "int x = func(Q)" to "int x = func((void*) Q)". i rewrite the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>	

using namespace std;

struct arg_struct {
        //double vec[100];
	arg_struct(){vec=new double[100];}
        double* vec;
	int length;
};

int func(void *argument)
{
	struct arg_struct args;
	args.vec = argument;
	//while(++args.length < 100)
	//	args.vec[args.length] = static_cast<double*>(argument)[args.length];
	cout << args.length << "\n\n";
        return 0;
}

int main()
{
	double Q[100];
	int x;
	x = func((void *)Q);
	//system("pause");
        return 0;
}


but i receive the same errors like before:

file.cpp: In function int func(void*):
file.cpp:15: error: invalid conversion from void* to double*


i tried :
1
2
3
4
5
6
7
8
9
10
11
...
int func(void *argument)
{
	struct arg_struct args;
	//args.vec = argument;
	while(++args.length < 100)
		args.vec[args.length] = static_cast<double*>(argument)[args.length];
	cout << args.length << "\n\n";
        return 0;
} 
...


but i receive the following errors :
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


i still have problem with the casting :(
*YOU* do not have cast problems,
your problem is that you just don't understand that array member can't be assigned to other array just with pointer:

your code:
args.vec = argument; this assigns only the first member of the Q array (I've told you that in my first post)

using for or while loop is first thing what u have to do and only then you can say "I'm having cast problem"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;

struct arg_struct {
        //double vec[100];
	arg_struct() : length(-1) {vec=new double[100];}
        double* vec;
	int length;
};

int func(void *argument)
{
	struct arg_struct args;
	while(++args.length < 100)
		args.vec[args.length] = static_cast<double*>(argument)[args.length];
	cout << args.length << "\n\n";
        return 0;
}

int main()
{
	double Q[100];
	int x;
	x = func((void *)Q);
	//system("pause");  /BTW. DO NOT USE SYSTEM("PAUSE") (THIS IS FOR NOOBS ONLY)
	cin.ignore();
    return 0;
}


SEE? now it work as you wanted,
however I see no good reason in casting functin parameter cos compiler will do that for you.

cheers!
Last edited on
¿is there a reason to use that prototype?
And that dynamic allocation just make leaks. (also the size is fixed, it is not justified)

Take a look at memcpy
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.
Last edited on
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.

The code is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>	

using namespace 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 ?
arg.vec[i] = static_cast<double>(argM[i]);

You can't do argM[i] because argM is a void* and you can't dereference a void pointer.

You have to cast it to a double* before you dereference it:

arg.vec[i] = static_cast<double*>(argM)[i]; // <- note the subtle difference


Of course if the function only works with doubles, that begs the question "why are you using void pointers in the first place"?
Last edited on
did you read examples provided above with litle more atention?

you code:
arg.vec[i] = static_cast<double>(argM[i]);

correct code:
arg.vec[i] = static_cast<double*>(argM[i]);

also I think this is incorrect too:
funcS((void *)&arg);

you'll have to use pointer to struct member.
@codekddy: your correct code is also incorrect. The [i] must be outside the cast (outside the parenthesis).
@Disch
yeah lol
I know, I've just copy the code on the fly and paste it lol
didn't look wel ^^

let me change my errors :)
1
2
args.vec[args.length] = static_cast<double*>(argument)[args.length];
arg.vec[i] = static_cast<double*>(argM)[i];
@newdomingo

when you're declaring a struct you can just declare it like any other type

in your code you have
 
struct arg_struct args;


you can change that to
 
arg_struct args;


edit: point (pun intended) taken
Last edited on
^ args is not a pointer.
Topic archived. No new replies allowed.