Is it possible to dynamically use scanf to prompt for user input ? I have the code shown below

scanf("%d %d %d %d", &myarray[row][0], &myarray[row][1], &myarray[row][2], &myarray[row][3])

which works fine when the number of columns is fixed but in my case it is not !

Recommended Answers

All 3 Replies

You could loop until all input was exhasted putting each next element into the next spot in the array on each loop

and yes ... you can also dynamically allocate memory for your array ... and expand the array if it needs more memory

You might like to see an example of a C vector container

http://developers-heaven.net/forum/index.php/topic,2580.0.html

Search for malloc, calloc and realloc functions. The only issues with malloc and calloc is if you don't know the maximum size of your dynamic array, but you can always try with realloc and allocate byte by byte(try to input, check if there is space, if not - reallocate memory by adding one more)

Ya you can use loop for iteration upto the number of columns and then u can acheive it just like this

for(i = 0;i < col;i++)
    scanf("%d",&myarray[row][i]);

and for dynamic allocation of column you can use malloc or calloc function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.