how do i change *test_obj to a value and not null in main i am confusedCode:typedef struct { int a =1; } test_struct; test_struct *test_obj = NULL; int main () { }
how do i change *test_obj to a value and not null in main i am confusedCode:typedef struct { int a =1; } test_struct; test_struct *test_obj = NULL; int main () { }
"test_obj" is a pointer that is not pointing to anything.
If you want to use the pointer to create a new instance of the struct, you need to allocate memory for it.
is this what do you need?Code:typedef struct{ int a; }test_struct; int main(){ test_struct.a = *value; return 0;
If you want to point to a structure that already exists, just assign its address to the pointer:
If you want to create a new instance of the struct, you need to allocate memory for it:Code:test_struct test; test_struct *ptr = &test;
Code:test_struct *test = malloc(sizeof(*test)); // be sure to free your memory when you're done with it
Typedef allow the shortcut of assigning pointer declarationCode:typedef struct{ int a; }test_struct, *test_pnt; test_struct mystruct = {1}; int main () { test_pnt myptr = &mystruct; myptr->a = 2; // change it to two }
Typedef allow the shortcut of assigning pointer declaration[/QUOTE]
Typing test_struct mystruct has the same number of keystrokes as struct test mystruct
Also, please don't hide a pointer using typedef for absolutely no reason at all; it's confusing!