Pointer Initialization and Usage in C++
This lecture covers the initialization of pointers in C++ along with examples and explanations of the reference and dereference operators. Learn about null pointers, void pointers, and the difference between reference and dereference operators in C++ programming.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Martin Kruli by Martin Kruli (v1.0) 1 19. 5. 2021
Pointers in C type *p = <pointer expression> nullptr pointer to nowhere (special value) Taken as reference (address) of existing value (&) Computed from another pointer (pointer arithmetic) Memory allocation function/operator Use only static array allocation on Arduino (e.g., int a[4]) Creates space for 1 int and labels it x int x = 42; Create pointer to int p (* declares pointer) int *p = &x; int y = *p + 54; Reads value at p address and uses it in addition (* dereferences pointer) by Martin Kruli (v1.0) 2 19. 5. 2021
Assuming the memory is already allocated! (pointers do not allocate by themselves) Pointers in C Pointer arithmetic ++p int *p2 = p+5 int *p int int int int int int Pointers and Arrays Array ~ pointer at beginning int buf[42]; Combined memory allocation (42 ints in a block) and pointer creation (buf is int*) Always use brackets [] when working with array, always use * when dereferencing pointer buf[i] *(buf + i) by Martin Kruli (v1.0) 3 19. 5. 2021
Strings in C Array of chars, terminated by 0 (zero char) const char *str = "Arduino Uno"; *str "\0" is added automatically A r d u i n o U n o \0 size_t some_function(const char *str) { size_t i = 0; while (str[i]) ++i; return i; } What does this function do? by Martin Kruli (v1.0) 4 19. 5. 2021
Pitfalls Uninitialized pointers int *p; May hold random random address always initialize! always initialize! Reaching beyond arrays int buf[5]; int *p = buf + 5; *p = 42; Huge mistake! buf array (where possibly some other variable is, but who knows?) Huge mistake! Writing data right after Strings in fix-sized buffers (like char buf[32]) Possible, but dangerous (must not overflow!!! Do not forget to allocate space for trailing zero! must not overflow!!!) by Martin Kruli (v1.0) 5 19. 5. 2021
Final task In ReCodEx Create a running text message on LCD Download prepared starter pack and open it Message is sent over via serial link Use SerialInputHandler class Display multiplex works like in the last assignment Text scrolls (4 chars are visible at a time) Advance position every 300ms Loop the scrolling (once its gone, start over again) Use pointers in your code !!! H i p p o i s g r e a t by Martin Kruli (v1.0) 6 19. 5. 2021
by Martin Kruli (v1.0) 7 19. 5. 2021