1
我在c編碼有點麻煩。我對這門語言很陌生,我知道java顯然更好,c中的字符串給我帶來最大的困擾。二維指針存儲字符串
當我實現這個代碼...
int num, n, i, j;
printf("How many students will you enter (min. 5)\n");
scanf("%d",&num);
char *fn = (char*)malloc(num * sizeof(char *));
char *ln = (char*)malloc(num * sizeof(char *));
for (n=0; n<num; n++)
{
*(fn+n) = (char *)malloc(20 * sizeof(char));
*(ln+n) = (char *)malloc(20 * sizeof(char));
}
printf("Enter students (firstName lastName score)\n");
for(i=0; i<num; i++)
{
scanf("%s %s", &fn[i], &ln[i]);
}
for (i=0; i<num; i++)
{
printf("%s %s\n", &fn[i], &ln[i]);
}
printf("You did it!");
它打印每個姓氏和名字的第一個字母,然後最後一個人進入我的全名。 例如,
Jane Doe
Greg Smith
爲用戶輸入輸出會
JGreg DSmith
Greg Smith
非常感謝你的幫助!
[Dont cast of malloc](http://stackoverflow.com/a/605858/5339899) – JackVanier
什麼是「二維指針」?在C中沒有這樣的東西。 – Olaf