[問題] 請問我的~結構指標陣列 出了什麼問題?

看板C_and_CPP (C/C++)作者 (張家安)時間12年前 (2013/09/29 03:17), 編輯推噓1(103)
留言4則, 2人參與, 最新討論串1/1
洪維恩: http://ideone.com/z1oY6M 我: http://ideone.com/OhSKQs 我修正後: http://ideone.com/SwF9VD 上面分別是洪維恩的範例, 和我的程式 我想仿照他第7行的寫法: char *ptr[3]={"Tom", "Lily", "James Lee"}; 宣告了以下的指標:(我的程式第20行) struct data *student[2]={{"john",100},{"tom",90}}; 結果在gcc顯示 我第20行發生錯誤 後來我把第20行的指標變數 *student 改成一般結構變數 student 就可以成功編譯了, 我想請各位前輩~ 為何我不能宣告 student 為指標呢? 謝謝 ===========錯誤訊息如下====== 20:8: warning: braces around scalar initializer [enabled by default] 20:8: warning: (near initialization for ‘student[0]’) [enabled by default] 20:8: warning: initialization from incompatible pointer type [enabled by default] 20:8: warning: (near initialization for ‘student[0]’) [enabled by default] 20:8: warning: excess elements in scalar initializer [enabled by default] 20:8: warning: (near initialization for ‘student[0]’) [enabled by default] 20:8: warning: braces around scalar initializer [enabled by default] 20:8: warning: (near initialization for ‘student[1]’) [enabled by default] 20:8: warning: initialization from incompatible pointer type [enabled by default] 20:8: warning: (near initialization for ‘student[1]’) [enabled by default] 20:8: warning: excess elements in scalar initializer [enabled by default] 20:8: warning: (near initialization for ‘student[1]’) [enabled by default] -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.116.46.220 ※ 編輯: plasmajohn 來自: 140.116.46.220 (09/29 03:23)

09/29 03:23, , 1F
因為那個語法是用來初始化一般結構用的
09/29 03:23, 1F

09/29 03:23, , 2F
你看的範例則是因為字串本身就"是"一個字元指標
09/29 03:23, 2F

09/29 03:24, , 3F
所以拿字元指標初始化字元指標當然沒有問題
09/29 03:24, 3F

09/29 03:25, , 4F
啊就 data type 不合...
09/29 03:25, 4F
短短幾行推文,看了好久終於明白了! 感謝樓上二位前輩! ※ 編輯: plasmajohn 來自: 140.116.46.220 (09/29 03:55)
文章代碼(AID): #1IHojRlb (C_and_CPP)
文章代碼(AID): #1IHojRlb (C_and_CPP)