Before I can get any longer in my life, I need to get answers to some important q's... One of these is the following:
I wonder why the following code doesn't work? I want to use the principles that are used in the code but I cannot figure out why it doesn't work?
Code:#include <stdio.h> #include <stdlib.h> #include <string.h> #define MSG_SIZE 100 // a simple struct typedef struct{ char *msg; }a_struct; // allocate memory for a struct void malloc_mystruct(a_struct *a_ptr){ // allocate for mystruct a_ptr = (a_struct *)malloc(sizeof(a_struct)); // allocate for msg a_ptr->msg = (char *)malloc(MSG_SIZE * sizeof(char)); } int main(void){ a_struct *a; char *txt="abc"; malloc_mystruct(a); strcpy( a->msg, txt); // This doesn't work.... sprintf( a->msg, "%s", txt); // This doesn't work either.... return 0; }