This line is meaningless:
s[0] = (test*)s[0];
As it assigns s[0]
to itself.
I suspect that you think it changes the type of s[0]
from a void*
to a test*
.
But that is not accurate.
Type-casts only affect how a variable is interpreted immediately, at the point of the type-cast.
It does not change the type of the variable in any lasting sense.
As a result, when your program reaches this line:
s[0]->a = 2;
s[0]
is still a void*
, and so de-referencing it for variable a
is not valid.
What you really want is:
((test*)s[0])->a = 2;