Dynamic allocation with scanf()

后端 未结 5 1950
孤街浪徒
孤街浪徒 2021-01-25 06:58

My question is exactly the same as this one. That is, I\'m trying to use scanf() to receive a string of indeterminate length, and I want scanf() to dyn

5条回答
  •  醉话见心
    2021-01-25 07:32

    I think, in real world, one need to have some maximum limit on length of user input.

    Then you may read the whole line with something like getline(). See http://www.cplusplus.com/reference/iostream/istream/getline/

    Note that, if you want multiple input from user, you don't need to have separate char arrays for each of them. You can have one big buffer, e.g. char buffer[2048], for using with getline(), and copy the contents to a suitably allocated (and named) variable, e.g. something like char * name = strdup( buffer ).

提交回复
热议问题