I understand that there’s no String data type in C. What I want to do is get an input string from the user and store it to a variable without having to define how many characters it needs to have. Can I do that using linked lists? I want to avoid putting it to a character array as much as possible so linked lists are the only thing I can think of but I don’t know how to.
Share
You could do it in a linked list, but a linked list of char will generally be an extremely inefficient data structure — each node will have one char and (at least) one pointer. In a typical case, you’re looking at 4 or 8 bytes for the pointer and one for the char, so you’re imposing a lot of overhead.
My immediate recommendation would be to put at least 16 characters in each node in your linked list to keep the overhead at least sort of reasonable. Of course, a dynamic array will usually be better still, but at least that will keep a linked list from being totally unreasonable.