2862 Shares

C string and array problem?

C string and array problem? Topic: How to write a linked list in c
June 20, 2019 / By Henrietta
Question: how can i make a string array which can allocate any number of string inputted. like having to input a very long word without declaring any array size. is there any other way not to ask for the user to give a string size
Best Answer

Best Answers: C string and array problem?

Earleen Earleen | 2 days ago
Complete solutions to your problem in C will be complicated. The most straightforward way to deal with this situation is to declared a reasonably-large array of characters (1000?) into which the input from the user goes. Read it one character at a time and you can avoid buffer overflow (ignoring input that would go beyond the 1000 characters). Then if you need an array of characters that is the right size for whatever was input, malloc can be used now that you know how long the string is. C has no dynamically-resizable data structures built-in. You could certainly write your own of course, like a linked list of characters, that can be grown with each character entered by the user, this gets around any need to predeclare a buffer of fixed size, but could easily be considered overkill just for reading a line of input from the console.
👍 272 | 👎 2
Did you like the answer? C string and array problem? Share with your friends

We found more questions related to the topic: How to write a linked list in c


Earleen Originally Answered: C string and array problem?
Complete solutions to your problem in C will be complicated. The most straightforward way to deal with this situation is to declared a reasonably-large array of characters (1000?) into which the input from the user goes. Read it one character at a time and you can avoid buffer overflow (ignoring input that would go beyond the 1000 characters). Then if you need an array of characters that is the right size for whatever was input, malloc can be used now that you know how long the string is. C has no dynamically-resizable data structures built-in. You could certainly write your own of course, like a linked list of characters, that can be grown with each character entered by the user, this gets around any need to predeclare a buffer of fixed size, but could easily be considered overkill just for reading a line of input from the console.
Earleen Originally Answered: C string and array problem?
u need to use a dynamic array with malloc() which returns a pointer to the allocated memory but CHECK that that pointer is NOT null

Celeste Celeste
u need to use a dynamic array with malloc() which returns a pointer to the allocated memory but CHECK that that pointer is NOT null
👍 120 | 👎 -3

Celeste Originally Answered: Java 6 array declaration?
Sure, you can do that. Correct your if thought you want logical operator == not assignment operator = if( x == 2 ) +add Wow, I hope they don't take my top contributor badge away: both answers are better than mine. In particular, I didn't even think of performing an operation inside the initialization list.
Celeste Originally Answered: Java 6 array declaration?
You could calculate the element in place, something like: myArray = {3,4,3+x,2,4} or myArray = {3,4,x==2?5:4,2,4} which ever one makes more sense to you.

If you have your own answer to the question how to write a linked list in c, then you can write your own version, using the form below for an extended answer.