Reading C Type Declarations
| Type | Quiz 4 Material |
|---|
Notes
C gibberish â English Website
C Data Types
Base Types
- Integer types(Signed, add[unsigned] to unsigned)
- [unsigned] char 8 bits
- [unsigned] short [int] 16 bits
- [unsigned] int 16/32 bits
- [unsigned] long [int] 32/64 bits
- [unsigned] long long [int] 64 bits
- Floating-point types
- float 32 bits
- double 64 bits
- long double 80/128 bits
- Aggregate types
- array
- struct
- union (later)
- Pointers (a special kind of integer)
Derived Types
- Every data has zero or more derived types:
- * â "pointer to..."
- [] â "array of..."
- [n] â "array of n ..."
- ... (...)(args) â "function taking args and returning ..."
Size
Depends on the platform. 16-bit and 64-bit computer has different size
Minimum size
- char â exactly 8 bits
- short int â at least 16 bits
- int â at least 16 bits
- long int â at least 32 bits
Sizeof()
sizeof is a compile-time constant reflecting the number of bytes held by a data type or instance
sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)
sizeof char is 1
Boolean
C historically did not have a Boolean data type. Instead, we can use integers to represent Boolean values.
- Any integer that is 0 is evaluated as False
- Any non-zero integer evaluates as True
- Also applies to a char
- NUL character â\0â evaluates to False
- Also applies to a pointer
- A memory address that is 0x0 is evaluated as False
String
- C does not include strings
- Strings are arrays of characters, end with an ASCII NUL(0 or '\0')
- And characters are 8-bit integers
- You can define a String like
char mystr[6];
- Intialization:
- char mystr[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
- char mystr[] = {'H', 'e', 'l', 'l', 'o', '\0'};
- char mystr[] = "Hello";
- The latter two are special cases, the compiler determines the length of the array from the initializer
- There are a number of library functions for dealing with strings, including strlen(), strcpy(), and strdup()
char s[6] = "hello";
// same as
s.stringz "hello"; // 6 memory locations, end with 0
//s is the memory address where the string starts- Never use sizeof(s) to determine string length.
- This tells the size of the array/pointer, not the actual length of the string.
- Use strlen(s). Must put: #include <string.h> â> import library
Declaration vs Definition
A declaration in C introduces an identifier and describes its type, be it a scalar, array, struct, or function. A declaration is what the compiler needs to accept references to that identifier (for type checking). You may have as many declarations of an identifier as you want within a scope as long as they are consistent.
A definition in C actually instantiates/implements this identifier. For instance, a definition allocates storage for variables or defines the body of a function. You may only have one definition of an identifier within a scope.
Declaration
There are two parts:
- Base Type
- This is the type (or struct s or a typedef), and optionally a storage class and/or a type qualifier
- The Base Type applies to all names up until the semicolon
- List of Declarations, separated by commas
- Each of these declares a type fr each identifier(the variable name)
- Each is based on the Base Type, but stands alone
- Consists of sensible combinations of "pointer to", "array of", or "function returning", ending with the Base Type
Rules on Declaration
- Remember the precedence of the declarators
- () and [] declarators get processed first
- gets processed last
- Parentheses change the precedence order (just as in expressions)
- Read or form the declaration from the inside out
- Example: int *(**f)() â> f is a pointer to a pointer to a function returning a pointer to int.
Parsing
determining which grammar productions (rules) were used to generate a sentence
Declaring vs Using Pointers and Arrays
*, (), and [] have different meanings in declarations and executable statements. Therefore, their behavior allows you to âunwindâ a type just as the declaration would wind it
Typedef
Typedef is a shortcut that creates a new alias for a type. It does NOT create a new type
- Creates a new type that is an alias for an existing one
- Typedef is a shortcut that allows you to create a new alias for a type
- It does not create a new type
- Start the declaration with âtypedefâ and put the alias name where you would put the variable name
- Donât let the syntax throw you: It just allows you to create another name for an existing type
Start the declaration with âtypedefâ and put the alias name where you would put the variable name
Examples
// Define an array of 5 struct a named b struct a b[5]; // Create a type alias for an array of 5 struct a typedef struct a sa5[5]; //Now we can use sa5 as a type name: sa5 c; // Note that b and c are the same type! // Another example typedef unsigned long size_t; size_t position; // What type is position? unsigned long
Reading and Forming Declarators
- Rule 1: Remember the precedence of the declarators
- () and [] declarators get processed first
- gets processed last
- Parentheses change the precedence order (just as in expressions)
- Rule 2: Read or form the declarations from the inside out
- Example: int *(**f)()
- f is a pointer to a pointer to a function returning a pointer to int.
- parsing: determining which grammar productions (rules) were used to generate a sentence
How to read a declaration
- Find the identifier name, e.g. "x" in "int x;"
- Read as far right as you can until:
- You hit a close paren ')', or
- You reach the end of the declaration
- Go back and read as far left as you can until:
- You hit an open paren '(', or
- You reach the beginning of the declaration
- If you hit parentheses, exit the parentheses
- Go to 2 and repeat until you read the whole declaration
Questions & Answers
- Translate the following declarations into English descriptions:
char **c[3];
Student & TA Solution
declare c as array 3 of pointers to pointers to char
char *(*d)[3];
Student & TA Solution
declare d as pointer to array 3 of pointers to char
int (*cmp)(int, int);
Student & TA Solution
cmp is a pointer to a function taking in two int parameters and returning an int
void (*f)(void);
Student & TA Solution
declare f as pointer to function taking no parameters and returning nothing
void *(*g)();
Student & TA Solution
declare g as pointer to function taking any parameters returning pointer to void
void *(**h[][6])[5];
Student & TA Solution
declare h as array of array of 6 pointers to pointers to an array of 5 pointers to a void
int (*(*q)(char **))[];
Student & TA Solution
q is a pointer to a function taking one pointer to a pointer to a char and returning a pointer to an array of int
- Super duper mega challenge problem. This is way harder than anything on the quiz; if you can answer the above, you'll be fine. Only look at this if you want a fun challenge!
int const * const (*(*c)(void *(*[2])(void *), const int (*)[2], const int *[2]))[3];
Student & TA Solution
declare c as pointer to function (array 2 of pointer to function (pointer to void) returning pointer to void, pointer to array 2 of const int, array 2 of pointer to const int) returning pointer to array 3 of const pointer to const int
- Translate the following English descriptions to C types. The first one has been done for you.
- DeclareÂ
c to be a pointer to arrays, where the elements of the arrays are pointers to integersint *(*c)[];
Student & TA Solution
int *(*c)[];
- Declare d to be an array of pointers to characters. The characters should not be able to be modified.
Student & TA Solution
const char (*d[]);
- Assume that we have declared aÂ
struct student. DeclareÂlookup to be a pointer to a function that takes an int and returns an array of student structs.
Student & TA Solution
struct student (*lookup)(int)[];
- Say you want to store multiple lookup functions. DeclareÂ
lookups to be an array of size 5, where each of the elements has the same type asÂlookup above.
Student & TA Solution
struct student (*lookups[5])(int)[];
- DeclareÂ
- In C, how many bits are the types short, int, and long?
- 16, 32, 64
- 32, 64, 64
- 32, 32, 64
- Any of the above
Prof Answer
D
- In C, a boolean value occupies
- 1 bit
- 8 bits
- 16 bits
- 32 bits
- Any size an integer can be.
Prof Answer
E
- You are given the C definition below. What is its type?
double **m[][];- Pointer to pointer to array of array of doubles
- Pointer to array of pointers to array of doubles
- Array of array of pointers to pointers to doubles
- Array of pointers to array of pointers to doubles
Prof Solution
C






