i am going to Write a program that stores strings and print the last two strings (i shoulde Use array of pointers).
this is my code
#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]);
}
}
it just stores the last string and prints it
this is the samle run
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
what is my mistake?
also if i replase
arr[i]=student_name;
with
strcpy(arr[i], student_name);
the sumple run will be
Enter the name 1 : ddf
Segmentation fault (core dumped)