简体   繁体   中英

Read in String from stdin using pointers

C pointer and string

Is it possible to read in string from stdin and store in a pointer if I didn't know the dimension of the string?

consider reading a whole line of input with fgets()

Not only is the ultimate length of input not known at run-time, but the length of chunks of input coming in are not known either. However the answer is yes, it is possible to do what you have asked, but will require dynamic memory allocation using either malloc or calloc AND realloc . Using these functions in conjunction will allow you to capture the input from stdin without knowing beforehand how much there will be.

Refer to the accepted answer to this question for details .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM