[英]What's the difference between struct { … } and struct { union { struct { … } } }?
[英]C pointers: what's the difference between struc* A, struct *A and struct * A?
我正在做一些研究以更好地理解C中的指針,但是我很難理解這些內容:“ struct * A”是結構上的指針嗎? 那么“ struct * A”是什么? 我見過有人在寫“ int const * a”,這是什么意思?
struc* A
,struct *A
和struct * A
什么區別?
它們是等效的(完全錯誤)。 C是一種自由格式的語言,空格無關緊要。
struct* A
是struct* A
的指針嗎?
不,這是(仍然)語法錯誤( struct
是保留關鍵字)。 如果在其中替換一個有效的結構名稱,那么它將是一個,是的。
int const * a
,這是什么意思?
這聲明a
為const int
的指針。
struct *A
, struct* A
和struct * A
都是一樣的東西,並且都完全錯誤,因為您缺少struct的名稱。
int const *a
與const int *a
相同,它表示指向const整數的指針。
另外: int * const a
是不同的,它表示const指針和非const整數。
它們都是相同的。
struct *A
= struct* A
= struct*A
= struct * A
正如其他人已經提到的, struct * A
等是不正確的,但相同。
但是,可以通過以下方式創建結構和指針:
/* Structure definition. */
struct Date
{
int month;
int day;
int year;
};
/* Declaring the structure of type Date named today. */
struct Date today;
/* Declaring a pointer to a Date structure (named procrastinate). */
struct Date * procrastinate;
/* The pointer 'procrastinate' now points to the structure 'today' */
procrastinate = &today;
另外,對於關於指針聲明的不同方法的第二個問題,“什么是int const * a
?”,這是我從Stephen G. Kochan改編自C的示例:
char my_char = 'X';
/* This pointer will always point to my_char. */
char * const constant_pointer_to_char = &my_char;
/* This pointer will never change the value of my_char. */
const char * pointer_to_a_constant_char = &my_char;
/* This pointer will always point to my_char and never change its value. */
const char * const constant_ptr_to_constant_char = &my_char;
當我第一次開始時,我會發現從右到左閱讀定義很有幫助,用“只讀”代替“ const”。 例如,在最后一個指針中,我將簡單地說:“ constant_ptr_to_constant_char是指向只讀char的只讀指針”。 在上面關於int const * a
問題中,您可以說,“'a'是指向只讀int的指針”。 似乎很愚蠢,但可以。
有一些變體,但是當您遇到它們時,可以通過在此站點附近搜索找到更多示例。 希望有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.