Reputation: 13
So I am writing a program that takes in any number of integers from stdin and I need to dynamically allocated enough space for the inputs.
This wouldn't be a problem for me except for two reasons: 1) You don't specify how many inputs there will be so I can't just use calloc along with the number of inputs without already reading through them and counting. 2) I can't use realloc to grow the size of the array.
Is there some way to count how many integers are in the input and then read through them again and put them into an array?
Upvotes: 1
Views: 415
Reputation: 19504
Use a linked-list. You can allocate new space as needed without moving the existing data.
Is there some way to count how many integers are in the input and then read through them again and put them into an array?
The only way I can think of to do that is to copy into a tmpfile()
as you count, then rewind()
the file and read again.
Upvotes: 0
Reputation: 62086
First, why can't you use realloc()
?
If you really can't, consider creating a linked list of integers with list elements defined something like:
typedef struct tNumber
{
int Number;
struct tNumber* Next;
} tNumber;
Is there some way to count how many integers are in the input and then read through them again and put them into an array?
There isn't any.
Upvotes: 1