AST21105Object-Oriented Programming and DesignTopic 3PointersLecture notes are based on textbook and various resources over the Internet
Variables and MemoryoVariables present in the source code are allocated in the main memory of the computer. Behind the scenes at runtime, each variable uses an area of the computer's memory to store its value.oA variable or data object has three properties: nits data type,nits value, and nits storage location (i.e. memory address).31057MemoryAddress100010041008Values inmemoryMemory
Memory AddressoIn reality, nmemory addresses are usually displayed as hexadecimal numbers. For example, you may see something likeo0073FEBC (8 digits; 4 bits each) for a 32-bit program, o000000D0B48FFB14 (16 digits) for a 64-bit program, oOn some platforms, you may also see a 12-digit address like 0x7ffeefbff560. This is because today's CPU and compilers use up to only 48 bits for memory addresses (a large enough memory address space, 248bytes = 256 terabytes, for a normal program nowadays).nmemory addresses used in your program are what we call "virtual addresses" (logical addresses); the operating system and the CPU hardware need to do an additional step to convert them to physical addresses (a real location on the RAM). 4
5What are Pointers?±a’MemoryAddress10001012100410081016Variables inmemory1020Memory1000If one variable contains the address of another variable, the first variable is said to point tothe second.Pointer / Pointer variable
6Declaration of Pointer VariablesoIf a variable is going to hold an address of another variable, it must be declared as follows.oActually, we can treat <type>*as aspecial typewhich is pointer type<type>* <variable name>; <type>*<variable name>;Syntax:where <type> is the type of the variable address that the pointer variable can store (e.g. int, char, double, user-defined type), <variable name>is the name of the pointer variableOR
7Declaration of Pointer VariablesoPointer examples:nRecall the syntax for declaring a pointer variable:<type>* <variable name>;// Declare a pointer that points to a variable of integer typeint* a;// the value of a is garbage but is not NULL// Declare a pointer that points to a variable of double typedouble* b;// the value of b is garbage but is not NULL// Declare a pointer that points to a variable of char typechar* c;// the value of c is garbage but is not NULL// It is no difference for you to put * close to type OR// close to nameint* d;// the value of d is garbage but is not NULLint*d;// same as above, no differenceWe will talk a bit more about NULL later!
8Pointer Operator & (Address-Of)oThere are twospecial pointer operators: & and *(Note: The * here doesn’t mean multiplication)oThe first operator, &is a unary operator (i.e. with single operand) that returns the memory address ofanother variablenUsage: &<variable name>oWe can think of & as returning "the address of"intvar1 = 5; // pint receives the address of var1int* pint = &var1;doublevar2 = 1.23;// pdouble receives the address of var2double* pdouble = &var2;