FCS_Module-4_Notes[Pointers]
FCS_Module-4_Notes[Pointers]
Introduction to Pointers:
A pointer is a variable that holds the address of another variable. The pointer
variable contains only the address of the referenced variable not the value
stored at that address.
Advantages of using pointers are as follows
1. Pointers are more efficient in handling arrays and data tables
2. Pointers are used with function to return multiple values
3. Pointers allow C to support dynamic memory management
4. Pointers provide an efficient tool for manipulating dynamic data structures
such as structures, linked list, stacks, queues, trees etc
5. Pointers reduce length and complexity of program
6. Pointers reduce program execution time and saves data storage space in
memory
Disadvantages of pointers
1. Program may crash if sufficient memory is not available at run time to store
pointers.
Pointer uses two basic operators
2. The indirection operator (*): It is used to accesses the object the pointer
points to.
Declaring a Pointer:
Data type: It specifies the type of the pointer variable that you want to declare
like (int, float, char, double or void)
* (Asterisk): tells the compiler that we are creating a pointer variable and
ptr_name specifies the name of the pointer variable.
Example:
int *p; // declares a pointer variable p of integer type
float *temp; //declares a pointer variable temp of float data type Now
consider,
example:
int x = 10;
int *ptr;
ptr = &x;
• Assuming that the complier assigns it memory locations 1003 and 1004, we
say the value of x=10 and address of x is equal to 1003. We can dereference
a pointer, i.e, refer to the value of the variable to which it points, by using ‘*’
operator as in *ptr.
pnum = &a;
printf("\n a = %d\n",*pnum); pnum = &b;
printf("b = %d\n",*pnum);
}
Output:
a=3
b=5
This statement requests the operating system to allocate two bytes of space in
memory and stores 100 in that location.
By using the address of (&) operator, you can determine the address of a variable
• When using pointers, unary increment (++) and decrement (--) operators have
greater precedence than the dereference operator (*). Therefore, the expression
• *ptr++ is equivalent to *(ptr++). So the expression will increase the value of ptr
so that it now points to the next element.
• In order to increment the value of the variable whose address is stored in ptr,
write (*ptr)++.
• A pointer variable can be assigned the address of another variable (of the same
type).
• A pointer variable can be assigned the value of another pointer variable (of the
same type). A pointer variable can be initialized with a null value.
Null Pointers
A null pointer which is a special pointer value that is known not to point
anywhere. This means that a NULL pointer does not point to any valid memory
address.
To declare a null pointer we may use the predefined constant NULL,
int *ptr = NULL;
we can always check whether a given pointer variable stores address of some
variable or contains a null by writing,
if ( ptr == NULL)
{
Statement block;
}
• Null pointers are used in situations if one of the pointers in the program points
somewhere some of the time but not all of the time.
• (int*)gp: This part involves type casting. It's casting the pointer gp to a pointer to
an integer (int*).
• *(int*)gp: Finally, it dereferences the casted pointer, retrieving the value stored
at the memory location pointed to by gp, but interpreting it as an integer.
Output:
Pointer to an Array:
A pointer to an array is a pointer that points to the whole array instead of the first
element of the array. It considers the whole array as a single unit instead of it being a
collection of given elements.
Syntax: type(*ptr)[size];
where,
• type: Type of data that the array holds.
#include <stdio.h>
int main() {
char x = 'P';
char arr[] = "Jain";
char *ptr_x = &x;
char *ptr_arr = arr;
printf("Value of x : %c\n", *ptr_x);
printf("Value of arr: %s\n", ptr_arr);
return 0;
}
Output: Value of x : P
Value of arr: Jain
Pointer to Pointer:
A pointer to pointer which is also known as a double pointer in C is used to
store the address of another pointer.
A "pointer to a pointer" is a form of multiple indirections or a chain of
pointers.
In "pointer to a pointer", the first pointer contains the address of the second
pointer, which points to the location that contains the actual value as shown
below –
A pointer variable can be initialized at the time of declaration, by assigning it the address
of an existing variable.
int x = 10;
int *y = &x;
Example:
#include <stdio.h>
int main(){
int a = 10, b = 20, c = 30;
int *ptr[3] = {&a, &b, &c};
for (int i = 0; i < 3; i++){
printf("ptr[%d]: address: %d value: %d\n", i, ptr[i], *ptr[i]);
}
return 0;
}
Output:
ptr[0]: address: 6422040 value: 10
ptr[1]: address: 6422036 value: 20
ptr[2]: address: 6422032 value: 30
Pointers to functions:
A pointer to a function point to the address of the executable code of the function. Use
pointers to call functions and to pass functions as arguments to other functions.
It holds the base address of function definition in memory.
Syntax: datatype (*pointername) ();
Example:
#include <stdio.h>
void show(int* p){
(*p)++;
}
int main(){
int* ptr, a = 20;
ptr = &a;
show(ptr);
printf("%d", *ptr);
return 0;
}
Application of Pointers
Pointers can be used to traverse and manipulate arrays and strings more
efficiently.
• Function Pointers:
• Structures:
• File Handling:
Pointers are used in file handling operations, enabling efficient reading and
writing of data to files.
• Callback Functions:
The arguments passed from command line are called command line arguments. These
arguments are handled by main () function.
Syntax: int main(int argc, char *argv[ ] )
Where,
argc counts the number of arguments. It counts the file name as the first argument.
The argv[] contains the total number of arguments. The first argument is the file name
always
Example:
#include <stdio.h>
void main(int argc, char *argv[] ) {
printf("Program name is: %s\n", argv[0]);
if(argc < 2){
printf("No argument passed through command line.\n");
}
else{
printf("First argument is: %s\n", argv[1]);
}
}
Run above the program using Windows from command line is as follows:
program.exe hello
Output:
Program name is: program
First argument is: hello