main() {
char names [5][20] = {
"rmaesh",
"ram",
"suresh",
"sam"
"ramu"
};
char *t;
t = names[2];
names[2] = names[3];
names[3] = t;
printf(" the array elemsnt are \n");
int i = 0;
for (i = 0; i < 5; i ++)
printf("\n%s", names[i]);
}
我收到以下錯誤,而編譯這個程序什麼是該類別中的代碼
stringarrary.c: In function ‘main’:
stringarrary.c:12:11: error: incompatible types when assigning to type ‘char[20]’ from type ‘char *’
names[2] = names[3];
^
stringarrary.c:13:11: error: incompatible types when assigning to type ‘char[20]’ from type ‘char *’
names[3] = t;
您尚未向t分配任何內存。你應該在最後使用'char t [20]'或'char * t = malloc(20)'和'free(t)'。你的代碼會導致分段錯誤。 –
此外'conio.h'不是標準文件(也不是clrscr) –
@ Mohit Jain但是''C編譯器'中不能使用'char * t = malloc(20)'語法。你可以使用'char * t =(char *)malloc(20)':D –