A pointer variable is a variable that holds the memory address of another variable. They are called pointers for the simple reason that, by storing an address, they point to a particular location in memory.
At the moment when a variable is declared, it must be stored in a concrete memory location. The programs do not decide where the variable is to be placed. That is done automatically by the compiler and the operating system at run time.
The declaration int x=3 tells the C compiler to
- Reserve space in memory to hold the integer value.(location/address is decided by the compiler/os)
- Associate the name x with this memory location.
- Store the value 3 in this location.
printf(“%d”,sizeof(x));
The value stored can be obtained with
printf(“%d”,x);
The memory location or address can be obtained with &
printf(“%p”,&x); // &x
is the address of the variable x.
The pointer
variable is declared with *(indirection) operator.
Eg:
int *p;
creates a pointer variable p which can store the address of an integer
variable.
p=&x ;
now p contains address of integer variable x ie; p points to variable x.
Because a
pointer holds an address rather than a value, it has two parts.
The pointer
itself holds the address.
The address
points to a value.
printf(“%p”,p)
will print the address it holds
printf(“%d”,*p)
will print the value it points to
pointers can
be used to
- Build faster and efficient code.
- Alternate/efficient way of accessing information stored in variables and arrays.
- Pass arrays and strings more conveniently from one function to another.
- Return more than one value from a function
- Dynamic memory allocation.
- Build complex data structures.( linked lists,
trees etc..)
In C programming, the & (address-of) and * (dereference) operators play important roles when working with pointers:
Address-of Operator (&):
The & operator is used to obtain the memory address of a variable.
It returns the memory address where the variable is stored in the computer's memory.
Example: &variable returns the address of the variable variable.
The * operator is used to access the value stored at the memory address pointed to by a pointer.
It is also used to declare pointers.
Example: *pointer accesses the value stored at the memory location pointed to by pointer
It is also used to declare pointers.
Example: *pointer accesses the value stored at the memory location pointed to by pointer
NULL Pointer
Pointers that do not point to any memory location. They can be created by assigning a NULL value to the pointer. A pointer of any type can be assigned the NULL value.
Syntax
data_type *pointer_name = NULL;
Void Pointer / Generic Pointer
Pointers of type void.
They do not have any associated data type.
Syntax:
void * pointer_name;
Constant Pointers
The memory address stored inside the pointer is constant and cannot be modified once it is defined. It will always point to the same memory address.
Syntax
data_type * const pointer_name;
Pointer to Constant
The pointers pointing to a constant value that cannot be modified are called pointers to a constant.
Syntax
const data_type * pointer_name;
Dangling pointer
A pointer pointing to a memory location that has been deleted (or freed) is called a dangling pointer.
The following program will illustrate the pointer indirection operator and dereferencing
#include <stdio.h>
int main()
{
int num=5;
int *ptr=#
printf(“the value and address of num is %d %p\n”,num,&num);
printf(“the value and address of num using pointer ptr is %d %p\n”,*ptr,ptr);
*ptr=15; // indirection, which stores value in variable num using the pointer ptr.
printf(“the value of num after indirect initialization is %d\n”,num);
}
int main()
{
int num=5;
int *ptr=#
printf(“the value and address of num is %d %p\n”,num,&num);
printf(“the value and address of num using pointer ptr is %d %p\n”,*ptr,ptr);
*ptr=15; // indirection, which stores value in variable num using the pointer ptr.
printf(“the value of num after indirect initialization is %d\n”,num);
}
the value and address of num is 5 0x7ffe6246e2fc
the value and address of num using pointer ptr is 5 0x7ffe6246e2fc
the value of num after indirect initialization is 15
The following program uses a call by reference mechanism to modify a variable value using pointers
#include <stdio.h>
void f(int *x) // x is a pointer to n
{
*x=*x+1; //accessing the actual content
}
int main()
{
int n=10;
f(&n); //calling the function with address/reference
printf("n in main =%d\n",n);
//note..... value of n changed in main
}
void f(int *x) // x is a pointer to n
{
*x=*x+1; //accessing the actual content
}
int main()
{
int n=10;
f(&n); //calling the function with address/reference
printf("n in main =%d\n",n);
//note..... value of n changed in main
}
The following program use pointers to add two numbers using a function add
int add(int *p1,int *p2)
{
return(*p1+*p2);
}
#include <stdio.h>
int main()
{
int a=10,b=12,sum;
sum=add(&a,&b);
printf("Sum=%d",sum);
}
Comments
Post a Comment