Arrays either have fixed or dynamic size in C, they can't have both. When taking user input, it is perfectly sensible to use a fixed size array, because taking "infinite input" doesn't make any sense.
If there's a lot of data to store, then you can copy it from the fixed size array to a dynamic one once the input is verified.
The only sensible way of allocating an array dynamically is to use malloc
(family of functions). It is done as char* arr = malloc(100);
, no need for casting of taking the size of a character. "I don't want to" isn't a great rationale, this is how the language works.
As for char **string; string = (char *)malloc(100 * sizeof(char));
it is simply wrong and doesn't make any sense. If you wish to declare an array of pointers it is char** arr = malloc(n * sizeof(char*))
. This only allocates room for the pointers though, not the data pointed at by them.
Alternatively you can use variable-length arrays (VLA) such as int n=100; char array[n];
but this isn't recommended practice beyond temporary store of small objects. VLA often get allocated on the stack and their storage duration is limited to the block where it was declare. Therefore excessive use of them might chew up all stack space and they also turn invalid soon as you leave the scope where you declared it.
For beginners, I'd say that malloc
is recommended over VLA.