我要写一个程序存储字符串并打印最后两个字符串 (我使用指针数组)。指针数组存储字符串c
这是我的代码
#include<stdio.h>
#include <string.h>
main()
{
char*arr[10];
char student_name[20];
int i,j;
for(i=0;i<10;i++) {
printf("Enter the name of student %d : ",(i+1));
scanf("%s",student_name);
arr[i]=student_name;
}
for(j=7;j<10;j++) {
printf("the name of student %d : %s\n",(j+1),arr[j]);
}
}
它只是存储的最后一个字符串并打印
这是samle运行
Enter the name 1 : qqq
Enter the name 2 : www
Enter the name 3 : eee
Enter the name 4 : rrr
Enter the name 5 : ttt
Enter the name 6 : yyy
Enter the name 7 : uuu
Enter the name 8 : ii
Enter the name 9 : ioo
Enter the name 10 : ppp
the name 9 : ppp
the name 10 : ppp
什么是我的错?
另外,如果我replase
arr[i]=student_name;
与
strcpy(arr[i], student_name);
的sumple运行将是
Enter the name 1 : ddf
Segmentation fault (core dumped)
欢迎来到Stackoverflow。你的问题很清楚,格式也很好。我希望你能得到一个快速的答案。 –