簡體   English   中英

C指針:struc * A,struct * A和struct * A有什么區別?

[英]C pointers: what's the difference between struc* A, struct *A and struct * A?

我正在做一些研究以更好地理解C中的指針,但是我很難理解這些內容:“ struct * A”是結構上的指針嗎? 那么“ struct * A”是什么? 我見過有人在寫“ int const * a”,這是什么意思?

struc* Astruct *Astruct * A什么區別?

它們是等效的(完全錯誤)。 C是一種自由格式的語言,空格無關緊要。

struct* Astruct* A的指針嗎?

不,這是(仍然)語法錯誤( struct是保留關鍵字)。 如果在其中替換一個有效的結構名稱,那么它將是一個,是的。

int const * a ,這是什么意思?

這聲明aconst int的指針。

struct *Astruct* Astruct * A都是一樣的東西,並且都完全錯誤,因為您缺少struct的名稱。

int const *aconst 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM