我目前在學習關於C語言中的struct
數據結構,以及如何用typedef
關鍵字前綴這個結構。這將導致實際結構的變量名被放置在不同的命名空間在幾個不同的引用解釋:沒有typedef關鍵字的結構體
Difference between 'struct' and 'typedef struct' in C++?
typedef struct vs struct definitions
但是,目前還不清楚,什麼是與例子發生那我有工作:
#include <stdio.h>
struct demo
{
short low_pass_vcf;
short filter_coupler;
short reverb;
short sequential;
} synth;
int main()
{
printf("Size of struct: %i\n", sizeof(struct demo));
printf("Size of struct: %i\n", sizeof(synth));
return 0;
}
在這個例子中,我可以通過synth
變量訪問struct
數據結構名稱;然而,在我看到的例子中,爲了讓我能夠做到這一點,struct
需要以typedef
開頭。在這個例子中,typedef
沒有被使用,但我仍然能夠通過synth
來引用這個結構。我想知道C到底發生了什麼事讓我做這件事?任何解釋將不勝感激。乾杯。
你熟悉用'typedef'呢? – ameyCU
'sizeof'產生'size_t'類型的結果。 'size_t'值的正確格式字符串是'%zu',而不是'%i'(用於'int')。 –
感謝'size_t'信息。根據我的理解,'typedef'的作用是它在單獨的名稱空間中創建一個變量,該變量允許在不需要使用'struct demo'的情況下引用該結構。不知道幕後是否有其他事情發生。 –