char str[] = "vaercly love iOS";//文本 char temp[20] = ""; char *p[3] = {0};//存储堆区的地址 int num = 0;//单词总个数 int j = 0; for (int i = 0; i < strlen(str) + 1; i++) { if (str[i] != ' ' || str[i] != '\0') { temp[j] = str[i]; j++; } if (str[i] == ' ' || str[i] == '\0') { temp[j] = '\0';//将空格替换为'\0' p[num] = malloc(sizeof(temp));//动态分配内存空间,将具体的空间地址存储到对应的指针数组元素中 strcpy(p[num], temp);//将单词拷贝到对应的堆区空间 j = 0; num++;//单词个数加1 } } for (int i = 0; i < num ; i++) { printf("%s\n", p[i]);//输出单词 free(p[i]); p[i] = NULL; }