为什么我们不能在 gets 和 puts 中使用箭头运算符?


您無法在未初始化的指標中讀取使用者輸入。而應有一個結構資料類型的變數,並在使用 → 運算子存取其內部元素之前,將其位址指定給指標

範例

#include <stdio.h>
struct example{
   char name[20];
};
main(){
   struct example *ptr;
   struct example e;
   puts("enter name");
   gets(e.name);
   ptr=&e;
   puts(ptr->name);
}

輸出

上述程式碼的典型結果

enter name Disha
You entered Disha

更新時間: 22-Jun-2020

121 次觀看

開啟您的 事業

通過完成課程獲得認證

馬上開始
广告