• Have Any Question?
  • contact@eitworld.com

C Pointers





Pointers in c programming is a special kind of variable that holds the address of the variable or data item and C Pointers use two operators that are & (address of ) for object address, and another is * (value at address) called as indirection operator. Pointers in c use%u for accessing the address of the pointer variable and it is also used for unsigned int. you can declare a pointer as follows:

Data_type *variable_name;
Example: int *p; //pointer to integer

Eitworld.com provides the much simple learning on pointers in c programming with examples.

  • A pointer is a special kind of variable that holds the address of the variable or data item.
  • With the help of pointer, you can access the memory location directly.
  • There are two operators used in pointer concept that are & (address of ), and another is * (value at address).
  • Address operator is used to determine the address of the object.
  • The * operator is called as dereferencing of indirection operator.
  • A pointer variable can be passed to the function as argument.
  • Using pointer, multiple values can be returned from the function.
  • Pointer is used to reduce the code and it improves the performance due to direct access of address of the variable.
  • In the pointer concept, %u is used to access the address of the pointer variable and it is also used for unsigned int.

Declaring a pointer

To declare a pointer, we use the * symbol. The basic syntax is as follows:

Data_type *variable_name;
Example:
int *p; //pointer to integer

Below is an example which shows the use of pointer to access the address of a variable and also the value present at that memory address:

#include<stdio.h>

void main ()
{
int num=20;
int *ptr; // declaration of pointer variable
ptr = # // store address of variable in pointer ptr
printf(“Address of num variable: %un”, &num);
/* address stored in pointer variable */
printf(“Address stored in ptr variable: %un”, ptr );
/* access the value using the pointer */
printf(“Value of *ptr variable: %dn”, *ptr );
}

In above example we have declared num variable of int type and initialize with value means int num = 20;
int *ptr; it is used to define the pointer variable.
ptr = # this statement is used to store the address of num variable into the pointer variable ptr.
%u format is used to get pointer address. It is also used for unsigned integer values.
printf(“Address of num variable: %un”, &num); this statement prints the address of the num variable.
printf(“Address stored in ptr variable: %un”, ptr ); this statement prints the address that is stored in the pointer variable.
printf(“Value of *ptr variable: %dn”, *ptr ); this statement prints the value of the pointer variable ptr.
The output of the program is below:

pointer

Pointer Arithmetic

  • C allows us to perform arithmetic operations on pointers.
  • These operations can be addition, subtraction, increment, decrement, and comparison.
  • The following examples show the arithmetic operations on pointer
Example 1 : Incrementing Pointer
#include
void main()
{
int number=30;
int *ptr; //pointer to integer
ptr=&number; //stores the address of number variable

printf(“Address of ptr variable is %u n”,ptr);
ptr=ptr+1;
printf(“After increment: Address of ptr variable is %u n”,ptr);
}

In the above program, we have declared an int variable with initialization menas int number=30;
int *ptr; this is used to declare a pointer variable ptr.
ptr = &number; here this statements stores the address of number variable.
printf(“Address of ptr variable is %u n”,ptr); this statement is used to print the address before incrementing.
ptr=ptr+1; this statement increment the value by one. Note that if we are using 32-bit system then the value of address is incremented by 2 and if we are using 64-bit system then the value of address is incremented by 4.

printf(“After increment: Address of ptr variable is %u n”,ptr);

this statement prints the address after incrementing the pointer variable.
Below is the output of the program:

pointer

Example 2 : Decrementing Pointer
#include
void main()
{
int number=30;
int *ptr; //pointer to integer
ptr=&number; //stores the address of number variable

printf(“Address of ptr variable is %u n”,ptr);
ptr=ptr-1;
printf(“After decrement: Address of ptr variable is %u n”,ptr);
}

In the above program, we have declared an int variable with initialization menas int number=30;
int *ptr; this is used to declare a pointer variable ptr.
ptr = &number; here this statements stores the address of number variable.
printf(“Address of ptr variable is %u n”,ptr); this statement is used to print the address before incrementing.
ptr=ptr-1; this statement decrement the value by one. Note that if we are using 32-bit system then the value of address is decremented by 2 and if we are using 64-bit system then the value of address is decremented by 4.

printf(“After decrement: Address of ptr variable is %u n”,ptr);

this statement prints the address after incrementing the pointer variable.
Below is the output of the program:

pointer

Pointer and Array

  • When an array is declared then the complier is responsible to allocate the address and the memory to the each element of the array in the contiguous memory location.
  • So we can say that array works like a pointer.
  • The following example shows array elements with their address.
#include<stdio.h>

void main()
{
int a[3]={1,2,3},i;
printf(“nArray-Element and Value Address”);
for(i=0;i<3;i++)
{
printf(“na[%d]t %d t %u”,i,*(a+i),(a+i));
}
}

In the above program, we have declared an array of int type with initialization of size 3 and declares a variable i means int a[3]={1,2,3},i;
printf(“nArray-Element and Value Address”); this statement prints a message like Array-Element and Value Address.
Now we have used for loop. Inside for loop printf(“na[%d]t %d t %u”,i,*(a+i),(a+i));
i is used for ith value
*(a+i) is used for accessing the array elements using the pointer.
(a+i) is used for the address of the array indexes.
The output of the program is shown below:

pointer

Array of Pointers

  • Array of pointers is used to point to an array of data items or data elements with each element of the pointer array pointing to an element or data item of the data array.
  • Data items can be accessed either directly in the data array using the index number, or indirectly by dereferencing (using *) the elements of the pointer array.
  • The advantage of the pointer array is that the pointers can be recorded without moving the data items.
  • We can declare an array of pointer as follows:
  • int *arp[3];
  • The below example shows the demo for accessing elements from array using array of pointer
#include<stdio.h>

#include<conio.h>

void main()
{
int a[3]={1,2,3},i;
int *arp[3];//array of pointer
for(i=0;i<3;i++)
{
arp[i]=&a[i];
}
printf(“Display elements using array of pointer:”);
for(i=0;i<3;i++)
{
printf(“n a[%d]=%d”,i,*arp[i]);
}
getch();
}

In the above program, we have declared and initialized an array int a[3] = {1,2,,3]; and declared a variable i.
int *arp[3]; this statement is used to create array of pointer.
In the first for loop, arp[i]=&a[i]; is used to assign the address of each element of array a[] into the array of pointer *arp[].
In the 2nd for loop, printf(“n a[%d]=%d”,i,*arp[i]); this statement prints the array elements.
Below is the output of the program:

pointer

Pointer To Pointer

    • In C programming, a pointer variable can be used to store the address of another pointer variable.
    • If one pointer variable has the address of another pointer variable then it is called as pointer to pointer.
    • The basic syntax of declaring pointer to pointer is as follows:
data_type ** pointer_variable;
  • The below example shows the concept of pointer to pointer :
#include<stdio.h>

#include<conio.h>

void main()
{
int a, *p1;
int **p2; // pointer to pointer
a=20;
p1=&a;
p2=&p1;
printf(“via *p1 :n The Value of a = %d n Address of a = %d”,*p1,p1);
printf(“n Via **p2 : n The Value of a = %dn Address of a=%d”,**p2,*p2);
getch();
}

In the above program, we have declared variable a of int type and a pointer variable p1 of int type means int a,*p1.
int **p2; this statement is used to define pointer to pointer.
a=20; here initialize the variable by 20.
p1=&a; this statement assign the address of variable into pointer variable p1.
P2=&p1; this statement set the address that hold by p1 to p2
Below is the output of the program:

pointer



Real Time Web Analytics