Converting a std::list to char*[size]

前端 未结 6 1295
温柔的废话
温柔的废话 2021-01-21 20:54

for some reason I cannot explain, every single item in the character array...is equal to the last item added to it...for example progArgs[0] through progArgs[size] contains the

6条回答
  •  耶瑟儿~
    2021-01-21 21:22

    You're assigning the same pointer (the address of the first element of the stack array item) to each element of progArgs, then repeatedly overwriting that memory. You can do:

    progArgs[count] = strdup(t->c_str());
    

    and get rid of the first two lines of the for body.

    strdup allocates memory, so you will have to free each element with free later. Also, you were not allocating a character for the NUL-terminator. You need would strlen + 1. However, this is not an issue with strdup, since it allocates for you.

提交回复
热议问题