471,082 Members | 915 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,082 software developers and data experts.

dynamic string lenght

I have to read input from the keyboard and save it as a string (but in C strings are acctually character arrays or something?) that I am going to manipulate later. That wouldn`t be a problem except there must be no artifical limit to the string lenght. It has to fit into the array size no matter if it is a million letters long so I can`t just make room for it with a char word[100]; declaration like I would usssually.

So how do I solve this and make the array be the size of whatever the input lenght is? I`m guessing it has to be something with malloc function, but I have no idea how exactly. Neither logic nor syntax wise.
Apr 8 '07 #1
2 2014
9,208 Expert Mod 8TB
Everything has a limit and this case is not different.

Just set a minimum length. Get that many characters from the keyboard. Allocate memory for those characters and copy from your buffer to your allocation. If you did not encounter the enter key, then accept the minimum legnth characters again. Attach the second fetch to the first (good place ofr a function here). Keep fetching until you have gotten all the characters.
Apr 8 '07 #2
Ok, just say:

Expand|Select|Wrap|Line Numbers
  1. char *word[100];
This means that you will have an array of 100 character arrays with no specific defined size of each element.
Apr 9 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

14 posts views Thread by Spare Change | last post: by
4 posts views Thread by Robert | last post: by
8 posts views Thread by cdolphin88 | last post: by
4 posts views Thread by javaalien | last post: by
1 post views Thread by Barok | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.