「。」與「。」有什麼區別?和「 - >」訪問C結構中的數據?我幾次嘗試都找不到任何區別。兩者都提供我訪問慾望數據「。」或「 - >」C struct accessor
回答
.
與結構一起使用。 ->
用於指針(指向結構)。
如果你的結構變量類型是指針Mystruct *mystructPtr
;
然後你應該使用->
(箭頭運算符),否則使用.
(點運算符)是好的。
6.5.2.3結構和聯合成員
約束
1的第一個操作數。運算符應具有原子的,合格的, 或不合格的結構或聯合類型,第二個操作數應該指定該類型的成員。
2的第一操作數中的 - >操作員應鍵入「」指針 原子,合格,或不合格的結構「」或「」指針原子, 合格或不合格的聯合「」,和第二個操作數應命名爲所指類型的 成員。
語義
3一個後綴表達式,後跟一個。運算符和標識符 指定結構或聯合對象的成員。值爲指定成員的第 ,95),如果第一個表達式爲左值,則該值爲左值。如果第一個表達式具有限定類型,則結果爲指定成員類型的合格版本 。
4後綴表達式後跟 - >運算符,標識符 指定結構或聯合對象的成員。值爲第一個表達式 指向的對象的指定成員的 ,並且是一個左值.96)如果第一個表達式是指向 限定類型的指針,則結果具有如此限定的版本指定的成員類型爲 。
struct MyStruct
{
int a;
}
MyStruct *st;
st->a = 10;
MyStruct st2;
st.a = 10;
有比指針structs/unions
->
操作和滑動structs/unions
或任何其它類型之間不存在連接,其他。 ->
正在訪問指針指向的struct/union
中的成員。意思是,創建與成員struct/union
之後,struct/union
成員可以通過兩種如果.
持有struct/union
本身或->
訪問,如果抱着pointer
到struct/union
例子:
// creating one instance of struct s, and a pointer to an instance of struct s. struct s is a struct holding one int called 'data'.
struct s{int data;}struct_s_instance, *struct_s_instance_pointer = malloc(sizeof(struct s));
struct_s_instance.data = 3; // access using the '.' operator
struct_s_instance_pointer->data = 4; // pointer access using the '->' operator
printf("%d-%d", struct_s_instance.data, struct_s_instance_pointer->data);
你不能data
使用.
(即struct_s_instance_pointer.data
)或data
使用->
(即struct_s_instance->data
)在struct_s_instance
中使用struct_s_instance_pointer
。這些是完全不同的東西。
注意給出的指針,如struct_s_instance_pointer
時,你可以derefernce它:*struct_s_instance_pointer
,然後運營商.
可以而且應該被使用:(*struct_s_instance_pointer).data
「*運算符和結構體,聯合體之間沒有聯繫*」 - 確定有 –
@FelixPalmen如何訪問結構體中的數據(而不是指針)使用-_> _? – CIsForCookies
如何使用' - >'來訪問*不是結構或聯合成員的對象? –
的->
運營商只是語法糖:
x->y
與
(*x).y
括號是必要的,因爲第e .
運營商的優先級高於運營商的優先級。
- 1. 的C指針問題:&(* struct->結構)
- 2. const C Struct數組struct struct數組
- 3. Xcode C++ Struct Order
- 4. accessor和mutator?
- 5. 私人領域與公共accessor在c + +
- 6. 這會被視爲Accessor嗎? (C++)
- 7. C struct裏面的結構struct
- 8. struct EMPLOYEE {...} em1,em2;或者struct EMPLOYEE {...};
- 9. C++ struct array copy
- 10. Lua和C-struct
- 11. C - struct problems - writing
- 12. C++ struct和container
- 13. 使用pycparser解析struct C中的struct?
- 14. OCCI - setDataBuffer +矢量<struct>
- 15. jquery accessor id vs class?
- 16. Objective C Class or struct?
- 17. Swig - 包裝C struct
- 18. 的std ::設置< struct >在結構C++
- 19. C++ STL的地圖,其關鍵是shared_ptr的<struct tm>
- 20. 二進制搜索矢量< struct S>在c + +?
- 21. Private Accessor類忽略通用約束
- 22. WSASend:發送int或struct
- 23. Swift - C函數和struct tm
- 24. struct struct
- 25. C++傳遞一個矢量<矢量<STRUCT>>用於修改
- 26. F# - >通用型 - 多不是struct
- 27. struct array vs object array c#
- 28. 如何編寫accessor和mutator?
- 29. MS UnitTest - Private Accessor not always recognized
- 30. accessor和mutator方法混亂
- >當您嘗試從指針調用smt到結構 時使用。當您創建對象並致電 –
如果您找不到任何區別,請嘗試用另一個替換另一個,而不更改其他任何內容,並查看您的程序是否仍在編譯:-) – dasblinkenlight
實際上沒有代碼,這兩個東西沒有區別。一個將永遠不能編譯 –