Allocating memory in char * struct

Posted by mrblippy on Stack Overflow See other posts from Stack Overflow or by mrblippy
Published on 2010-04-21T03:49:43Z Indexed on 2010/04/21 3:53 UTC
Read the original article Hit count: 166

Filed under:

hi, im trying to read in a word from a user, then dynamically allocate memory for the word and store it in a struct array that contains a char *. i keep getting a implicit declaration of function âstrlenâ so i know im going wrong somewhere.

struct class


{
  char class_code[7];
  char *name;

 };

 char buffer[101];
 struct unit units[1000];

 scanf("%s", buffer);

 units[0].name = (char *) malloc(strlen(buffer)+1);
 strcpy(units[0].name, buffer);

© Stack Overflow or respective owner

Related posts about c