How to declare an array with an arbitrary size

后端 未结 4 1595
失恋的感觉
失恋的感觉 2021-01-14 10:32

Ok, this is a C programming homework question. But I\'m truly stuck.

I ask the user to input words, and then I insert the input into an array, but I can\'t have any

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-14 10:49

    0) obviously you will need multiple buffers, so you will need a list like structure: perhaps a record with char array 100 chars and a pointer to next structure 1) You need to capture the words char by char and store them in your buffer 2) once the buffer is full you allocate another record, chain it with the previous one and keep going until you are out of mem or the process is over.

    That should be better performance than realloc function. I believe malloc is trying to give contious block of memory. Therefore the list like structure will be faster and work better.

提交回复
热议问题