W6 - POINTERS Flashcards
W6-1: What is an ADDRESS?
_ Every program VARIABLE occupies a UNIQUE ADDRESS in memory throughout its lifetime.
_ The ‘address of’ operator (&) applied to a variable’s identifier evaluates to the ADDRESS of that variable in memory.
W6-2: What is a POINTER?
_ A variable that holds an ADDRESS
_ To store the a variable’s address
=> define a pointer of the variable’s type and assign the variable’s address to that pointer.
A pointer takes the form: type *identifier;
1/ type * is the type of the pointer.
2/ identifier is the name of the pointer.
W6-3: What is the DEREFERENCING or INDIRECTION operator.
_ The * operator stands for ‘data at address’ or simply ‘data at’
_ This operator applied to a pointer’s identifier evaluates to the value in the address that that pointer holds.
W6-4: What is NULL Address?
_ Each pointer type has a special value called its NULL value
_ The constant NULL is an implementation defined constant that contains this null value (typically, 0).
_ This constant is defined in the and header files.
_ It is good style to initialize the value of a pointer to NULL before the address is known.
_ EX: int *p = NULL;
W6-5: PARAMETERS
A function can receive in its parameters not only DATA VALUES but also ADDRESSES of program variables.
W6-6: Pass by Address
To change the ORIGINAL values,
=> pass the ADDRESSES of their variables instead of their values.
_ Use these addresses to access the ORIGINAL values and change them from within the function.
W6-7: Multiple RETURN VALUES
_ C FUNCTION syntax only allows for the RETURN of a SINGLE value.
_ If program design requires a function that returns more than ONE value,
=> do through PARAMETER POINTERS that hold the ADDRESSES of the variables that receive the MULTIPLE return values.
_ FUNCTIONS that return values through their parameters can RESERVE their return values for reporting any ERROR codes produced by the function.