GAME OF CODE — DAY 3
Before going to start my blog I wanna tell you that I find many beginners find pointers difficult to understand and they are afraid to pointers! If you were also afraid of pointers…..So do not be afraid of pointers! Read the blog to build your concept.
Pointer is a very important concept in c++. We need to learn and consolidate repeatedly to understand it.
So let’s start !
WHAT ARE POINTERS :
Addresses are represented by pointers, which are symbolic representations of them. A pointer is a variable in C++ that stores the address of another variable. Pointers, like ordinary variables, have a data type. A pointer variable is a variable that points to a specific memory location which is pointed to by another variable.
A pointer of type integer, for example, can hold the address of an integer variable. A character type pointer can hold the address of a character type variable.
C++ pointers are easy and fun to learn. Some C++ tasks are performed more easily with pointers, and other C++ tasks…
To know C++ pointers, you must first know how computers store information. When you create a variable in a C++ application, it is allotted a certain amount of memory on the computer. This variable’s value is saved in the specified place. The & (reference) operator in C++ is used to determine where the data is stored in the computer memory. The address of a variable is returned by the operator……. If x is a variable, for example, &x returns the variable’s address.
POINTER DECLARATION SYNTAX :
— The name of the pointer variable should be used as a variable name.
— The asterisk used to declare a pointer is the same as the asterisk used to perform a multiplication operation.
— The asterisk indicates that the variable is a pointer.
In C++, here’s an example of a proper pointer declaration:
int *x; // a pointer to integer
double *x; // a pointer to double
float *x; // a pointer to float
char *ch // a pointer to character
REFERENCE OPERATOR (&) DEFERENCE OPERATOR (*) :
The address of the variable is returned by the reference operator (&).
The dereference operator (*) allows us to retrieve a value from a memory address.
Consider the following situation: If we have a variable named num, it is saved at location 0x234 with the value 28. 0x234 is returned by the reference operator (&). The dereference operator (*) returns a value of 28.
POINTERS TO FUNCTIONS :
It is possible to dynamically choose a function during runtime in C++. A function can also be sent to another function as an argument. As a result, the function is handed to the caller as a pointer. Callback Function refers to the function’s pointer. Function pointers come in two flavors in C++:
1. Static member function pointers
2. Non-static member function pointers
Function Pointer in C++ - javatpoint
As we know that pointers are used to point some variables; similarly, the function pointer is a pointer used to point…
POINTER TO OBJECT :
A pointer in C++ can point to an object that was generated by a class. Object Pointers come in handy when it comes to generating objects at runtime. As a result, we may use an object pointer to access an object’s public members.
POINTERS OF VARIABLES :
It is possible to allocate or re-assign memory space as desired. Pointer variables make this feasible. The address of a pointer variable in the computer’s memory is pointed to by another variable.
THIS POINTER :
This pointer is a special keyword in C++ that represents an object that calls a member function. This pointer is a reference to the object for which the function was invoked. All member functions take this pointer as an implicit parameter.
TYPES OF POINTERS :
Dangling, Void , Null and Wild Pointers - GeeksforGeeks
A pointer pointing to a memory location that has been deleted (or freed) is called dangling pointer. There are three…
1) NULL POINTERS :
If there isn’t a specific location to be assigned, the pointer variable can be set to NULL. It should be done at the same time as the declaration. A null pointer is a pointer that has no value. It has no value and is specified in several standard libraries, such as iostream.
Example :- int*p = NULL;
2) WILD POINTERS :
An uninitialized pointer is a wild pointer. It may crash the program.
Example :- int*p; // wild pointer
3) DANGLING POINTER :
A pointer that points to a variable that goes out of scope (or) doesn’t exist in memory.
4) GENERIC POINTER :
A pointer which is declared with void type.
Example:- WAP to show the use of a NULL pointer.
ADVANTAGES OF POINTER :
1) The pointer is used to retrieve strings, trees, and other objects and is utilized with arrays, structures, and functions to minimize code and enhance speed.
2) Using a pointer, we can return several values from a function.
3) You can access any memory placed in the computer’s memory with it.
4) Pointers aid in the reduction of program complexity.
5) The use of pointers enhances the speed of a program’s execution.
APPLICATION OF POINTERS :
In C++, functions can only return one value. — Furthermore, the function call stack is used to allocate all variables defined in the function. — All stack variables are deleted as soon as the function returns. — Arguments to functions are passed by value, which means that any changes made to the variables have no effect on the value of the variables that are supplied.
¡ A pointer is a variable that stores the address of another variable.
¡ Each pointer has a data type that is valid.
¡ A pointer is a representation of a memory address in symbols.
¡ Pointers let programs generate and manipulate dynamic data structures while simulating call-by-reference.
¡ A similar notion is used by arrays and pointers.
¡ The base of the array is indicated by the array name.
¡ Assign a NULL to a pointer variable if there is no precise address to which it should be assigned.
¡ If you wish to provide a pointer to the address of an array, don’t use an ampersand (&).
You learned from the previous chapter, that we can get the memory address of a variable by using the & operator: string…
Clap for yourself!! You know about pointers and are ready to solve the pointers problem keep learning, keep killing the question with your concept.