c - Dynamic memory allocation after scanf -
is possible have dynamic memory allocation in string read scanf without first declaring array?
it not possible dynamically allocate memory string
after having read scanf
, scanf
needs pointer. means must allocate memory first, in order give argument scanf
.
you can following example :
char *str; str = malloc(x*sizeof(char)); //where x number of characters want allocate if (str != null) int result = scanf("%s", str);
in other words, pass allocated pointer scanf
.
note : should careful input give, might cause buffer-overflow if give string larger allocated space.
Comments
Post a Comment