Try these simple changes to your original code: (builds and runs without using malloc()
or calloc()
)
(comments in-line to explain)
#include <stdio.h>
#include <stdlib.h>
typedef struct{
int x;
int y;
}structure;
typedef struct{
structure test1;
}TEST; //use CAPITALS to distinguish typedefed struct for readability
TEST test; //use typedef to create instance of TEST
void function(TEST *trying){
trying->test1.x = 5;
printf("%d\n", trying->test1.x);
}
int main(){
TEST *mystruct; // create local instance of TEST
mystruct = &test;//initialize mystruct pointer to point to beginning of physical struct
function(mystruct);
system("pause");
return 0;
}