Dynamic memory allocation and structures

advertisements

So i'm learning structures and am trying inputting a string with dynamic memory allocation.

Here is what I have so far:

  typedef struct{
    char foo[81];
  } TYPE;

 void function(TYPE get[]){
   char a[81];
   scanf("%s", a);

   get->foo = malloc(strlen(a)+1*sizeof(char)); //this line produces an error that it is not assignable

   strcpy(get->foo,a);

   return; 

 }

I'm not sure what is wrong with that statement, any help would be well appreciated.


foo is an array object and not a pointer so you can not use the operation

get->foo = (char*)calloc(strlen(a)+1,sizeof(char));

you are trying to assign a (void *) to type char[81], which is totally incompatible. Lvalue should be pointer in this case.