This document provides a cheat sheet on C++ pointers. It covers topics such as:
1) Array of pointers, pointer vs array, pointers to pointers, pointers to functions, returning pointers from functions, NULL pointers, and arrays of strings.
2) Key differences between pointers and arrays are explained, such as pointers can be used to access array elements similar to indexes but pointers can also be reassigned to point to other objects.
3) A pointer to a pointer allows for multiple indirection or chains of pointers by taking the address of another pointer.
So in summary, this cheat sheet covers the essential C++ pointer concepts and syntax through examples in 3 sentences or less.
This document provides a cheat sheet on C++ pointers. It covers topics such as:
1) Array of pointers, pointer vs array, pointers to pointers, pointers to functions, returning pointers from functions, NULL pointers, and arrays of strings.
2) Key differences between pointers and arrays are explained, such as pointers can be used to access array elements similar to indexes but pointers can also be reassigned to point to other objects.
3) A pointer to a pointer allows for multiple indirection or chains of pointers by taking the address of another pointer.
So in summary, this cheat sheet covers the essential C++ pointer concepts and syntax through examples in 3 sentences or less.
by Nima (nimakarimian) via cheatography.com/113429/cs/21694/
Array of Pointers Pointer vs array Pointer to pointer
int *ptr[arraysize] = array of pointers, int b[10]; int A pointer to a pointer is a
pointing to int *bptr; form of multiple indirection or bptr=b; OR a chain of pointers Pointer to function bptr=&b[0]; int var; int (*FuncPTR)(int a,int b); *(bptr+3) // shows int *ptr; //called funcptr is a pointer the value of b[3] int **pptr; to function bptr+3 // points var = 3000; // actually is used as a to &b[3] ptr = &var; // take the parameter of another func and //an array can be address of var can pass any func to the desired used like a pointer pptr = &ptr;// take the func as a parameter with this too -> *(b+3)‐ address of ptr using address of method =value of b[3] operator & int func1(int); //pointer to an int func2(int); array can be used Return pointer from functions int func3(int (*FuncPTR)(int)); like an array -> int * getRandom( ) { now we can pass func1 or func2 bptr[3] = value of static int r[10]; to func3 ; b[3] return r; } Array of Pointers to functions NULL pointer // main function to call above void (*f[3])(int)= int *ptr = NULL; defined function. {function1,function2,function3}; //The value of ptr int main () { // f is an array of pointers , is 0 int *p; pointing to functions of type if(ptr) // succeeds p = getRandom(); void which all of them take one if p is not null } parameter of type int . if(!ptr) // succeeds if p is Array of strings null char *color= {"red","blue","yellow','green"}; passing pointers to //color is an array of pointers functions , pointing to the first WHEN PASSING character of each string ARGUMENTS unsigned long sec; getSeconds( &sec ); IN FUNCTION HEADER void getSeconds(u‐ nsigned long *par)
By Nima (nimakarimian) Published 29th January, 2020. Sponsored by Readable.com
Last updated 29th January, 2020. Measure your website readability! Page 1 of 1. https://readable.com