class: center, middle ### COMP26020 Programming Languages and Paradigms Part 1: C Programming *** # Introduction to Pointers --- # Program Memory Layout - All of the program's code and data are stored somewhere in memory - The area of memory accessible to the program is its **address space** - It is a large array of contiguous bytes - **Addresses** are indexes in that array
-- - Note that this is
virtual
memory: - Size of the address space is independent of the amount of RAM - Each program gets its own address space --- # Program Memory Layout - **Address of a variable**: the address in memory of the first byte used to store that variable ??? - So, as I said, all the code executed and the data manipulated by the program reside in memory - Each variable has an address, which is the address of the first byte of its memory location -- ```c int glob = 12; // glob's address is 0x3300 char string[] = "abcd"; // string's address is 0x2f0 typedef struct { int member1; float member2; } mystruct; int main(int argc, char **argv) { mystruct ms; // ms's address is 0x120 ms.member1 = 42; ms.member2 = 4.2; printf("ms member1: %d, member2: %f\n", ms.member1, ms.member2); return 0; } ```
--- # Addresses - Use the `&` operator to get the address of a variable ```c int glob = 12; char string[] = "abcd"; typedef struct { int member1; float member2; } mystruct; int main(int argc, char **argv) { mystruct ms = {1, 2.0}; // Addresses are printed with the "%p" format specifier. On most modern systems an // address is a 64-bit value, and "%p" prints it in hexadecimal prefixed by "0x", // for example "0x12345" (the exact output format is implementation-defined) * printf("ms address: %p, glob address: %p, string address: %p\n", &ms, &glob, &string); return 0; } ``` .codelink[
`09-pointers-introduction/ampersand.c`
] --- # Pointers .center[***Pointer*: variable whose value is an address**] .center[(possibly the address of another variable)] -- name:ptrintro - Declaration: `
*
;` ```c int v = 42; int *ptr = &v; ``` --
--- template: ptrintro
--- template: ptrintro
-- - We say that `ptr` ***points to*** `v` --- # Pointers - **Through a pointer it is possible to access the variable it points to** - This is called **dereferencing** - It is done with the indirection operator `*` -- ```c int v = 42; int *ptr = &v; printf("value pointed to by ptr: %d\n", *ptr); // 42, dereference ptr to read v's value printf("v's value: %d\n", v); // 42 *ptr = 0; // dereference ptr to write to v printf("value pointed to by ptr: %d\n", *ptr); // 0 printf("v's value: %d\n", v); // 0 ``` .codelink[
`09-pointers-introduction/pointer-update.c`
]
--- name:ptr # Pointers ```c int glob = 12; double glob2 = 4.4; typedef struct { int member1; double member2; } mystruct; int main(int argc, char **argv) { mystruct ms = {55, 2.23}; int *ptr1 = &glob; double *ptr2 = &glob2; mystruct *ptr3 = &ms; /* Print each pointer's value (the address it holds) and the pointed-to value */ printf("ptr1 = %p, *ptr1 = %d\n", ptr1, *ptr1); printf("ptr2 = %p, *ptr2 = %f\n", ptr2, *ptr2); printf("ptr3 = %p, (*ptr3).member1 = %d, (*ptr3).member2 = %f\n", ptr3, (*ptr3).member1, (*ptr3).member2); } ``` .codelink[
`09-pointers-introduction/pointer.c`
] --- template: ptr
--- template: ptr
--- template: ptr
--- template: ptr
--- # Summary - A pointer: variable that stores an address corresponding to a memory location - The data at that location can be accessed through the pointer (dereferencing) ---- .center[Feedback form: https://bit.ly/3fKzIzr]
??? - Let's recap - A pointer is a variable that holds an address corresponding to a particular memory location - And we can access that location through the pointer - In the next video we will see how useful pointers are