9.9 KiB
title | TARGET DECK | FILE TAGS | tags | |
---|---|---|---|---|
Declarations | Obsidian::STEM | c17 |
|
Overview
C declarations were designed so that the declaration of an object looks like the use of the object. This isn't quite true - keywords like volatile
and const
only exist in declarations - but for the most part, this philosophy can be leveraged to read C declarations.
Declarators
A declarator in C is roughly an identifier along with pointers, function brackets, or array indications. Pointers will look like one of:
*
* const
* volatile
* const volatile
* volatile const
whereas direct declarators will look like one of:
identifier
identifier[size]
identifier(args)
(declarator)
%%ANKI
Basic
What two qualifiers can be used in a pointer declaration?
Back: const
and volatile
.
Reference: Van der Linden, Peter. Expert C Programming: Deep C Secrets. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
END%%
Declarations
A declaration consists of at least one type-specifier (e.g. signed short
), storage class (e.g. static
), and/or type qualifier (e.g. const
) as well as one or more declarators.
%%ANKI Basic How many declarators does a declaration have? Back: At least one. Reference: Van der Linden, Peter. Expert C Programming: Deep C Secrets. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
END%%
%%ANKI Basic How many declarations does a declarator have? Back: N/A. Reference: Van der Linden, Peter. Expert C Programming: Deep C Secrets. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
END%%
%%ANKI Basic Which part of the following are declarators?
int* a, b;
Back: * a
and b
.
Reference: Van der Linden, Peter. Expert C Programming: Deep C Secrets. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
END%%
%%ANKI Basic Which part of the following are declarations?
int* a, b;
Back: The entire line is a single declaration. Reference: Van der Linden, Peter. Expert C Programming: Deep C Secrets. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
END%%
%%ANKI Basic Which part of the following declaration is the declarator?
const int *const x;
Back: *const x
Reference: Van der Linden, Peter. Expert C Programming: Deep C Secrets. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
END%%
%%ANKI Basic Which part of the following declaration is the type specifier?
const int *const x;
Back: int
Reference: Van der Linden, Peter. Expert C Programming: Deep C Secrets. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
END%%
%%ANKI Basic Which part of the following declaration is the type qualifier?
const int *const x;
Back: The first const
.
Reference: Van der Linden, Peter. Expert C Programming: Deep C Secrets. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
END%%
%%ANKI
Basic
What is the type of x
in the following?
const int *const x, y;
Back: const int* const
Reference: Van der Linden, Peter. Expert C Programming: Deep C Secrets. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
END%%
%%ANKI
Basic
What is the type of y
in the following?
const int *const x, y;
Back: const int
Reference: Van der Linden, Peter. Expert C Programming: Deep C Secrets. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
END%%
%%ANKI
Basic
What is the type of x
in the following?
#define int_ptr int *
int_ptr x, y;
Back: int *
Reference: Van der Linden, Peter. Expert C Programming: Deep C Secrets. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
END%%
%%ANKI
Basic
What is the type of y
in the following?
#define int_ptr int *
int_ptr x, y;
Back: int
Reference: Van der Linden, Peter. Expert C Programming: Deep C Secrets. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
END%%
%%ANKI
Basic
What is the type of x
in the following?
typedef int_ptr int *
int_ptr x, y;
Back: int *
Reference: Van der Linden, Peter. Expert C Programming: Deep C Secrets. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
END%%
%%ANKI
Basic
What is the type of y
in the following?
typedef int_ptr int *
int_ptr x, y;
Back: int *
Reference: Van der Linden, Peter. Expert C Programming: Deep C Secrets. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
END%%
Declarations can be read by complying with the precedence rules outlined below:
- Find the name of the declaration.
- Obey the following precedence rules:
- Parentheses grouping together parts of a declaration
- Postfix operators
()
and[]
- Prefix operator: the asterisk
*
denoting "pointer to"
- If
const
and/orvolatile
keyword is next to a type specifier, it applies to the type specifier. Otherwise it applies to the pointer asterisk on its immediate left.
%%ANKI
Basic
In the precedence rules for C declarations, what available postfix operators are there?
Back: ()
and []
.
Reference: Van der Linden, Peter. Expert C Programming: Deep C Secrets. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
END%%
%%ANKI
Basic
In the precedence rules for C declarations, what available prefix operators are there?
Back: Just *
.
Reference: Van der Linden, Peter. Expert C Programming: Deep C Secrets. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
END%%
%%ANKI
Basic
In the precedence rules for C declarations, what available type qualifiers are there?
Back: const
and volatile
.
Reference: Van der Linden, Peter. Expert C Programming: Deep C Secrets. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
END%%
%%ANKI
Basic
What is the type of foo
in the the following declaration?
char *const *(*foo)();
Back: A pointer to a function returning a pointer to a const
pointer-to-char.
Reference: Van der Linden, Peter. Expert C Programming: Deep C Secrets. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
END%%
%%ANKI
Basic
What is the type of foo
in the the following declaration?
char *const *foo();
Back: A function returning a pointer to a const
pointer-to-char.
Reference: Van der Linden, Peter. Expert C Programming: Deep C Secrets. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
END%%
%%ANKI
Basic
What is the type of foo
in the the following declaration?
int (*(*foo)(void))[3]
Back: A pointer to a function (accepting void
) returning a pointer to an array of int
s.
Reference: Van der Linden, Peter. Expert C Programming: Deep C Secrets. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
END%%
%%ANKI
Basic
What is the type of foo
in the the following declaration?
const int (* volatile foo)[64]
Back: A volatile
pointer to an array of const int
s.
Reference: Van der Linden, Peter. Expert C Programming: Deep C Secrets. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
END%%
%%ANKI
Basic
What is the type of foo
in the the following declaration?
const int * const foo;
Back: A const
pointer to a const int
.
Reference: Van der Linden, Peter. Expert C Programming: Deep C Secrets. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
END%%
%%ANKI
Basic
What is the type of foo
in the the following declaration?
const int * foo;
Back: A pointer to a const int
.
Reference: Van der Linden, Peter. Expert C Programming: Deep C Secrets. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
END%%
%%ANKI
Basic
What is the type of foo
in the the following declaration?
int const * foo;
Back: A pointer to a const int
.
Reference: Van der Linden, Peter. Expert C Programming: Deep C Secrets. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
END%%
%%ANKI
Basic
What is the type of foo
in the the following declaration?
int * const foo;
Back: A const
pointer-to-int.
Reference: Van der Linden, Peter. Expert C Programming: Deep C Secrets. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
END%%
%%ANKI
Basic
What is the type of foo
in the the following declaration?
char *(*foo[10])(int **);
Back: An array of pointers to functions (accepting int **
) returning pointer-to-char.
Reference: Van der Linden, Peter. Expert C Programming: Deep C Secrets. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
END%%
%%ANKI
Basic
What is the type of signal
in the the following declaration?
void (*signal(int sig, void (*func)(int)))(int);
Back: A function (accepting an int
and void (*)(int)
) returning a pointer to a function (accepting an int
) returning void
.
Reference: Van der Linden, Peter. Expert C Programming: Deep C Secrets. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
END%%
Bibliography
- Bryant, Randal E., and David O'Hallaron. Computer Systems: A Programmer's Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.
- “ISO: Programming Languages - C,” April 12, 2011, https://port70.net/~nsz/c/c11/n1570.pdf.
- Van der Linden, Peter. Expert C Programming: Deep C Secrets. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.