When to use Pointers in C++

Apart of dynamic memory allocation, data structures etc there is more to the reason why pointers must be used. There are situations where you do not want to pass the actual values or is it not required. If you want to return more than one value from a function, you make use of it. It also increases the execution speed as they tend to reduce the complexity and length of a program. Pointers efficiently handle the data types. If we use a pointer array for a character string, it will save data resultantly. Apart from trees, linked lists and other data structures require the use of pointers. When using pointers, we don’t need to make a copy of the object that is passed (like in pass by reference). When we are using big arrays in function arguments, it is very beneficial. Also, if one function allocates the memory, the other function can modify it without using global variables. When implementing strings, pointers provide great ease, meaning incrementing the pointer would point to the next memory location of the array taking away the worry to calculate that how many bytes to be moved for each data type. Resizing is possible when using pointers. For instance, creating an array using malloc and assigning it to a pointer, then to resize it create a new memory using malloc and copy the contents over. This feature is important when implementing sparse data structures (DP Project Development, 2015).


DP Project Development. (2015) What is the importance of pointers? Available at: https://www.urbanpro.com/c-language/what-is-the-importance-of-pointers-people/3535455 (Accessed: 11 July 2017)


Leave a Reply

Your email address will not be published. Required fields are marked *