Have you ever wondered how exactly stuff goes behind the scenes in every programming language?…
Whenever you are writing a code two things that come into the picture, one is time complexity and the second one is space complexity. Let’s figure out space complexity minimization using pointers.
What comes into your mind when the word pointer is said? Well. A pointer is nothing but to show someone or something in general. You can understand it better by observing the analogy below.
In the above image, we can see a person is pointing to the whiteboard. Let’s imagine a person as a pointer and the whiteboard as a variable.
So the definition of pointer goes like this,
A pointer is a variable that holds the memory address of the other variable. It is one of the most exhilarating and distinct features of programming language. It also adds flexibility. (memory address will be a hexadecimal number). Remember Pointers play a vital role in the implementation of linear data structures like stacks, queues, and linked lists.
Pointer and variable in memory
Declaration of pointer:
Following is the syntax to declare a pointer.
data_type *pointer_name;
Here “data_type” is the type of data we want to store in this particular pointer. Asterisk(*) is used to show that “pointer_name” is a pointer variable. pointer_name needs a memory location and stores the address of another variable.
Initialization of pointer variable:
Following is the syntax to initialize a pointer.
pointer_name = &variable_name;
The process of assigning the address of a variable to a pointer variable is known as initialization. Here Ampersand(&) is an address operator. Address of the “variable_name” is assigned to the “pointer_name” pointer variable.
Combination of declaration and initialization is also allowed in the programming language. The syntax is as follows:
data_type *pointer_vaiable = &variable_name;
A pointer variable points to a data type (like int, double, string, etc.,) of the same type only. For instance, consider the below code which is written in the C programming language.
int v=10;
int *ptr=&v;
printf(“%d\n”,v); // outputs 10, value of variable v
printf(“%d\n”,*ptr); // outputs 10. value of variable v
printf(“%d\n”,ptr); // outputs 0x7ffc9f7662f0, memory address of variable v
printf(“%d\n”,&v); // outputs 0x7ffc9f7662f0, memory address of variable v
Dangling pointer:
A pointer must be initialized with NULL or zero value when it not used. Otherwise, it will become a dangling pointer which is dangerous.
Do’s while using pointers:
- Always store only the address of a variable of the same type in a pointer variable.
- One can assign the address of a variable to a pointer variable.
- One can also assign the value of another pointer variable to a pointer variable.
Don’ts while using pointers:
- Do not store the address of a variable of one type in a pointer variable of another type.
- Do not use a pointer variable before assigning it, because the pointer variable contains garbage values before it is initialized.
- Never assign a numeric constant to a pointer variable.
- Never assign the address of a variable to a variable of a basic data type.
- A value cannot be assigned to an arbitrary address.For instance, &variable=50; is illegal.
Summary:
- Memory is a sequential collection of storage cells with each cell having an address value associated with it.
- The pointer is used to store the memory address as a value.
- Pointer variable stores the memory address of another variable.
- The pointer variable allocates memory only for the pointer variable, not for the variable to which it is pointing.
- Two pointers cannot be added.
THANK YOU :)
-Preksha