Allocating memory for a char pointer that is part of a 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
5:03 UTC
Read the original article
Hit count: 189
c
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[4];
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