这是多个字符串,进行逆序输出,但保证字符串内部不被打乱的程序。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#pragma warning(disable:4996)
int main()
{
int i, n;
char* color[20];
char str[15];
i = 0;
printf("enter n:");
scanf_s("%d", &n);
printf("please input words:");
scanf_s("%s", str,sizeof(str));
//color[0] = str;
//puts(color[0]);
//printf("%d", strlen(str));
while (i < n-1) {
color[i] = (char*)malloc(sizeof(char) * (strlen(str) + 1));
strcpy(color[i],str);
printf("%d", i);
i++;
printf("next");
scanf_s("%s", str,sizeof(str));
}
printf("ouput");
for (i = n - 1; i >= 0; i--) {
printf("%s ", color[i]);
free(color[i]);
}
return 0;
}
在执行操作的时候,在最后的输出总是没有,color[]这个指针字符串组里似乎卡死了,有没有大师指点迷津