📊CAB202 | 2024 S1
Microprocessors and Digital Systems
Table Of Contents
Week 1: Microcontroller Fundamentals
Week 2: Digital Representations and Operations
Week 3: Microcontroller Interfacing
Week 4: Assembly Programming
Week 5: Introduction to C
Week 6: Pointers, Arrays, Functions and Scope
Week 7: C for Embedded Systems
Week 8: OVERVIEW SUMMARY
Week 9: Timers: PWM and ADCs
Week 10: Serial Communication and Strings
Week 11: State Machines, Debouncing and Debugging
Week 12: Serial Protocol Design and Parsing
Week 13: Review
CAB202 Microprocessors and Digital Systems
Describe what CAB202 is about
Week 1: Microcontroller Fundamentals
Week 2: Digital Representations and Operations
Week 3: Microcontroller Interfacing
Week 4: Assembly Programming
Week 5: Introduction to C
Week 6: Pointers, Arrays, Functions and Scope
Pointers in C, a pointer is a special type of variable that can store the address of another variable in memory. For example, if you have a variable called num that stores the value 10, you can create a pointer called ptr that stores the address of num. This means that ptr points to num, or in other words, ptr references num.
To declare a pointer, you need to specify the type of the variable that it points to, followed by an asterisk (*) and the name of the pointer. For example, if you want to declare a pointer to an integer (int), you can write:
int *ptr; // Pointer to int
This code declares a pointer variable named ptr, but this variable is not a int itself. It is a pointer that can be used to point to a int variable. You can think of a pointer as a signpost that shows the way to another variable.
Internally, pointers store a memory address, which is a number that identifies a location in the computer's memory where some data is stored. For example, if num is stored at the memory address 1000, then ptr will store the value 1000 as well. You can use the ampersand (&) operator to get the address of a variable.
int num = 10; // Declare and initialise a int variable
int *ptr; // Declare a pointer to int
ptr = # // Assign the address of num to ptr
This code assigns the address of num to ptr, which means that ptr now points to num. You can use the printf function to print the address of a variable using %p format specifier. For example:
printf("Address of num: %p\n", &num); // Print the address of num
printf("Value of ptr: %p\n", ptr); // Print the value of ptr
This code will print something like
Address of num: 0x3e8
Value of ptr: 0x3e8
Note that the actual memory address may vary depending on your system and compiler. The 0x prefix indicates that the address is in hexadecimal (base 16) notation, which is commonly used to represent memory addresses.
To access the value of the variable tha ta pointer points to, you can use the asterisk (*) operator again, but this time as a dereference operator. This operator returns the value of the variable located at the address specified by its operand. For example:
printf("Value of num: %d\n", num); // Print the value of num
printf("Value of *ptr: %d\n", *ptr); // Print the value of *ptr
This code will print
Value of num: 10
Value of *ptr: 10
As you can see, changing the value of *ptr also changes the value of num, because they are the same variable You can also make a pointer point to different variable by assigning a new address to it. For example:
int num1 = 10; // Declare and initialise a int variable
int num2 = 20; // Delcare and initialise another int variable
int *ptr; // Declare a pointer to int
ptr = &num1; // Make ptr point to num1
printf("Value of *ptr: %d\n", *ptr); // Print the value of *ptr
ptr = &num2; // Make ptr point to num2
printf("Value of *ptr: %d\n", *ptr); // Print the value of *ptr
This code will print:
Value of *ptr: 10
Value of *ptr: 20
As you can see, the value of *ptr changes depending on which variable it points to. You can also use pointers to pass variables by reference to functions, or to access elements of arrays using pointer arithmetic, but those are more advanced topics.
Week 7: C for Embedded Systems
Week 8: OVERVIEW SUMMARY
Week 9: Timers: PWM and ADCs
Week 10: Serial Communication and Strings
Week 11: State Machines, Debouncing and Debugging
Week 12: Serial Protocol Design and Parsing
Week 13: Review
Last updated