我只是在看這個例子從Deitel:C:在使用指針時如何分配內存?
#include <stdio.h>
struct card {
char *face;
char *suit;
};
int main(void)
{
struct card aCard;
struct card *cardPtr;
aCard.face = "Aces";
aCard.suit = "Spades";
cardPtr = &aCard;
printf("%s%s%s\n%s%s%s\n%s%s%s\n", aCard.face, " of ", aCard.suit,
cardPtr->face, " of ", cardPtr->suit,
(*cardPtr).face, " of ", (*cardPtr).suit
);
system("pause");
return 0;
}
我看到有燒焦的指針,但從來沒有想過,你可以使用char *
節省串...
的問題是:內存是如何處理在這裏,因爲我沒有看到任何東西像char word[50]
。
請注意,現在將字符串文字分配給像這樣的char *類型的變量是個壞習慣。你應該改用'const char *'變量,因爲字符串文字不能在運行時修改(也不應該修改)。 –