Module 10: Variables & Pointers Flashcards
type modifiers
change the width of data types
name of an array
acts as the starting address where the array is stored in memory; the equivalent of an assembly label
casting
allows programmer to forcibly convert one data type to another
typedef
allows you to give known type a new name
pointer
variable that can only contain the memory address of another variable
dereference
the act of looking up the value that the pointer points to
- operator
the “address operator” in C. if it precedes any variable, it will determine its runtime address
void
officially a data type but really more of a keyword to indicate the absence of data
void* pointer
a typeless pointer, so it can point to any type of data. one limitation is that it cannot be dereferenced; you would have to cast it to a typed pointer in order to dereference it. the void* pointer’s purpose is to allow for the passage of a memory address but not to allow dereferencing
implicit cast
smaller to bigger conversions are implicit; compiler does this for you
explicit cast
bigger to smaller conversions you must do explicitly
int a = 5 ;
float b = 5.0 ;
a = (int) b ;
note that the b is truncated, fraction is lost, no rounding
why do we need pointers in C?
- helps us to pass addresses to functions instead of data
- recall that in C we pass copies of local variables to functions in order to ensure their local scope is protected
- pointers allow us to break scope and send address of a local variable
pointers vs. arrays
name of array: a label for an address in data memory that contains data
pointer: a variable that contains a memory address of a variable that contains data
you cannot change the address of an array (it is a label!)
you can change the address held onto by a pointer
you can use the [ ] operator as a dereferencing operator on a pointer
- helpful if pointer points to an array
- don’t let that confuse you into thinking that a pointer is an array!
you can do pointer arithmetic on a pointer but NOT on an array!
passing the name of an array is the same as passing a memory address
intitializing pointers
ALWAYS initialize! this will help to avoid unexpected behavior in your program.
constant in C
#define - you can change the next time you compile #define statements are like assembly directives (no space allocation)
const types - constants that never change
space allocation!
CONST applied to pointers
pointer to a constant:
we CAN change what the pointer points to but not the value of what it points to
constant pointer to a variable:
we can’t change what pointer points to but we can change the value of what it points to
constant pointer to a constant variable:
we can’t change what pointer points to
we can’t change the value of what it points to
3 basic regions of data memory in C
- stack (local variables, arguments, returns)
- global/static region (global vars, static vars)
- heap (dynamic space)
2 basic storage classes
automatic variables: lose their values when their block terminates (since they are on the stack)
-arguments, return types, local variables
static variables: retain values between invocations
-global variables are a type of static variable