Skip to content

Instantly share code, notes, and snippets.

@Zelmoghazy
Created February 26, 2025 20:38
Show Gist options
  • Save Zelmoghazy/d97440141bcea0256cec2f3536640840 to your computer and use it in GitHub Desktop.
Save Zelmoghazy/d97440141bcea0256cec2f3536640840 to your computer and use it in GitHub Desktop.
How declarations work in C
int a; // a evaluates to int -> a is an int
int *a; // *a (dereferencing) evaluates to int -> a is a pointer to int
int a(); // a() evaluates to int -> a is a function that returns int
int *a(); // () has higher precedence ≃ int *(a()) -> deref a() evaluates to int -> a is a function that returns pointer to int
int (*a)(); // (*a)() evaluates to int -> a is a pointer to function that returns int
int a[10]; // a[i] evaluates to int -> a is an array of 10 integers
int *a[10]; // [] has higher precedence ≃ int *(a[10]) -> deref a[i] evaluates to int -> a is an array of 10 pointers to int
int (*a)[10]; // (*a)[i] evaluates to int -> a is a pointer to an array of 10 integers
int a()[10]; // [] has higher precedence than () -> INVALID (functions can't return arrays)
int (*a())[10]; // (*a())[i] evaluates to int -> a is a function returning pointer to array of 10 integers
int (*a[10])(); // (*a[i])() evaluates to int -> a is an array of 10 pointers to functions returning int
/* ---------------------------------------------------------------------------------------------------------------------*/
T const; // const of type T
int const a; // a is a const integer
int const *a; // a is a pointer to const integer
int * const a; // a is a const pointer to an integer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment