C Language Variables and Pointers Flashcards
Type Modifier
Change 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 typedef struct{ int numerator; int denominator; } fraction;
fraction my_frac;
Using typedef for basic types
typedef unsigned char BYTE;
BYTE b1;
Pointer
variable that can only contain the memory address of another variable.
Does not contain data, however variable at memory address might contain data.
Is a variable too with an own memory address, hence can have a pointer to a pointer
Always one line in memory
Dereference:
act of looking up the value that the pointer points to
& operator
the “address operator” in C. If it proceeds 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 vs Explicit Cast
Smaller to bigger conversions are implicit; compiler does the casting for you:
int a = 5 ;
float b = 5.0 ;
b = a ; // implicit cast
Bigger to smaller conversions are not implicit; you must cast explicitly: int a = 5 ; float b = 5.0 ; a = (int)b ; // explicit cast, decimal is truncated // (fraction is lost – no rounding) ○ When going from bigger to smaller, the data is truncated so you must know what you are doing!
Why we need pointers in C:
○ One good use is to help us 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 to arrays in C
Pointers vs Label
○ The difference:
■ The name of an array is a LABEL for an address in data memory that contains data.
■ A pointer is a VARIABLE that contains a memory address of a variable that contains data.
○ You can change the address held onto by a pointer.
■ You cannot change the address of an array (because it is essentially a label)! You were not able to change a Label in Assembly once set, why should you be able to do it in C.
○ 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.
■ You cannot do pointer arithmetic on an array! See the above rules as to why.
○ Remember, an array’s name is basically the label for a memory address.
○ Passing the name of an array is the same as passing a memory address!
Why always initialize a pointer
You should always initialize a pointer in order to avoid unexpected behavior in your program.
Ways to define constants in C
#define you may change the next time you compile #define statements are like assembly directives i.e. don’t get space allocated ○ const types are for constants that “never change” e.g. pi ■ Note: const variables do have space allocated!
Const Modifier: Pointer to constant
We can change what the pointer points to but not the value of what it points to.
const char *ptr
Const Modifier: Constant pointer to a variable
We can’t change what pointer points to but we can change the value of what it points to.
char const *ptr
Constant pointer to a constant variable
■ We can’t change what pointer points to,
■ And we can’t change the value of what it points to!
const char const *ptr
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. note local variables can be declared static. Stored in Global region
■ Global variables are a type of static variable. -> stored in global region