How do I type cast a void pointer to a struct array? Here is my code:
typedef struct{
int a;
double b;
} myStruct;
void Func1(void * Array1);
int main(){
myStruct S1[5];
S1[0].a = 1;
S1[0].b = 2.3;
S1[1].a = 2;
S1[1].b = 3.4;
Func1(S1);
return 0;
}
void Func1(void * Array1){
myStruct S2[5];
S2[0] = (myStruct *)Array1[0];
}
I get compile errors in Func1
for assigning S2[0]
. How do I typecast the Array1
correctly?
[]
operator has a higher precedence that a (cast)
operator. Thus you have to use additional parenthesis:
S2[0] = ((myStruct *)Array1)[0];
or use a pointer:
myStruct* a = Array1 ;
S2[0] = a[0];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.