C Reference Cheat Sheet Page 3

ADVERTISEMENT

C Reference Cheat Sheet
by
Ashlyn Black
via
Structures (cont)
Unions
A strctName structure type pointer,
Defining
struct strctName
*ptrName;
ptrName.
A union type uName with two members, x & y.
union uName{int
Size is same as biggest member size.
Shorthand for defining strctName and
x; char y[8];}
struct strctName{ type
declaring varName as that structure
a; type b; } varName;
Declaring
type.
A variable vName as union type uN.
union uN vName;
A variable varName as structure type
struct strctName
Accessing
strctName and initialising its members.
varName = { a, b };
Members cannot store values concur​ r ently.
vName.y[int]
Accessing
Setting y will corrupt x.
Member x of structure varName.
varName.x
Unions are used for storing multiple data types in the same area of
Value of structure pointer ptrName
ptrName->x
memory.
member x.
Enumer​ a tion
Bit Fields
Defining
Declares x with two members a and b,
struct{char a:4, b:4}
both four bits in size (0 to 15.)
A custom data type bool with two possible
x;
enum bool {
states: false or true.
false, true };
Array members can't be assigned bit fields.
Declaring
Type Defini​ t ions
A variable varName of data type bool.
enum bool
Defining
varName;
Abbreviating a longer type
Assigning
typedef unsigned short uint16;
name to uint16.
Variable varName can only be assigned values
varName = true;
Creating a newType from a
typedef struct structName{int
of either false or true.
structure.
a, b;}newType;
Evaluating
Creating an enumerated bool
typedef enum typeName{false,
Testing the value of varName.
if(varName ==
type.
true}bool;
false)
Declaring
Pointers
Variable x as type uint16.
uint16 x = 65535;
Structure y as type newType.
Declaring
newType y = {0, 0};
Pointers have a data type like normal variables.
type *x;
By Ashlyn Black
Published 28th January, 2015.
Sponsored by
Last updated 20th April, 2015.
Measure your website readability!
Page 3 of 13.

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education