我有一些代码在使用 sprintf 将 aa 指针复制到字符串时进行堆栈转储。我正在尝试将动物的内容复制到一个名为 output 的新指针数组中。但是,我得到了一个堆栈转储。
输出中应该有以下内容:新动物兔子新动物马新动物驴
我会以正确的方式解决这个问题吗?
非常感谢
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void p_init(const char **animals, char **output);
int main(int argc, char **argv)
{
char *animals[] = {"rabbit", "horse", "donkey", '\0'};
char **prt_animals = animals;
char *output[sizeof(*animals)];
/* print the contents here */
while(*prt_animals)
{
printf("Animal: %s\n", *prt_animals++);
}
/* copy and update in the output buffer */
p_init(*&animals, *&output);
getchar();
return 0;
void p_init(const char **animals, char **output)
{
while(*animals)
{
sprintf(*output, "new animal %s", *animals);
*output++;
}
}