Let's learn java programming language with easy steps

Sunday, 12 November 2017

How to Use Pointers in C

Pointer in C Programming

In this article, We are going to learn what is a pointer and how to use pointers in c language with simple examples so that you can easily understand the pointer concept in c programming language.

Let's start with the definition of the pointer in c programming language and then take a lot of examples of c pointer to understand better.


What is Pointer in C Language?

Pointer is a special type of variable which stores or hold the address of another variable. Every variable has a particular address in a memory. We can know the address of variable and pointer easily by using some specifiers.


Advantages of Pointers in C

There are some advantages of pointer concept in c language.
  • Pointer reduces the length of the code and improves the performance because it can access address of a variable directly.
  • Pointer increases the processing speed.
  • Pointer saves memory.
  • Using pointer, We can return multiple values from any function.
  • Using pointer, You can access any memory location.

Symbols in C Pointer

There are some symbols or operator which is used in c pointer concept.

(1) Symbol - &(ampersand sign)

The name of this symbol is "address of operator" which is used to display the address of a variable.

(2) Symbol - *(asterisk sign)

The name of this symbol "indirection operator" which accesses the value at the address.


Address of Operator - &(ampersand sign)

In this example, We will print the address of variable 'x' by using the address of operator i.e & and there we will use %u format specifier for displaying the address of a variable.

For example:

#include<stdio.h>
#include<conio.h>
void main()
{
int x = 50;
clrscr();
printf("Value of variable x is : %d\n ", x);//print value of x i.e 50
printf("Address of variable x is : %u ", &x);//print address of variable x
getch();
}

Output: Value of variable x is : 50
             Address of variable x is : 65524

                                                                 OR

You can also use %p format specifier for display variable's address in hex format.

#include<stdio.h>
#include<conio.h>
void main()
{
int x = 50;
clrscr();
printf("The value of variable x is : %d\n ", x);//print 50
printf("Address of variable x is : %p", &x);//print address in hex format
getch();
}

When you will convert 65524 (decimal) to hex number, it will give you FFF4 in result. 

Now we are able to know the address of any variable and now it is time to know how to hold or store these variable's address into another variable, for this we will use the pointer in c programming language.


How to declare pointers in C?

By using *(asterisk sign) we can declare pointers in c language.

For example:

int *a;//pointer to integer type

char *b;//pointer to character type

double *c;//pointer to double type

float *d;//pointer to float type

Let's understand pointer in c language with example.


Example of Pointer in C

In this example, We will store the address of a variable 'x' into pointer variable '*p" and then print the address and value of a variable by using pointer variable.

#include<stdio.h>
#include<conio.h>
void main()
{
int x = 400;//variable x
int *p;//pointer variable
clrscr();
p = &x;//store the address of x variable 

printf("Value of variable x : %d\n ", x);//print 400
printf("Address of variable x : %p\n ", &x);//print address of x
printf("Address of variable p : %p\n ", p);//print address of p
printf("Value of variable p : %d ", *p);//print value of p

getch();

}
}

Output: Value of variable x : 400
             Address of variable x : FFF4
             Address of variable p : FFF4
             Value of variable p : 400

Note: The data type of both variable(x) and pointer(*p) variable must be same. 

For example: 

int x = 50;//integer data type 
int *p;//integer data type


Another example of Pointer in C

Pointer in C programming

In the above image, Here 'a' is a simple variable with value 40 and with memory address fff4 and 'p' is a pointer variable which stores and holds the address of variable 'a' i.e fff4. Here fff2 is the memory address of pointer variable.

By using the address of a variable 'a' into pointer p we can print the address and value of variable 'a'.

#include<stdio.h>
#include<conio.h>
void main()
{
int a = 40;
int *p;
clrscr();
p = &a;
printf("value of a : %d\n ", a);
printf("address of a : %x\n", &a);
printf("address of p : %x\n", p);
printf("address of p : %x\n", &p);
printf("value of p : %d ", *p);

getch();
}
}

Output: value of a : 40
             address of a : fff4
             address of p : fff4
             address of p : fff2
             value of p : 40

You can use %x format specifier for display address of the variable.


Null Pointer in C

A pointer that is not assigned any value but NULL is known as a Null pointer in c programming language and this done at the time of variable declaration.

For example:

int *p = NULL;


In this article, We discussed what is pointer in c programming language and how to use pointers in c and what is null pointers with simple and easy examples. 

Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on Google+

Follow javatutorial95 on twitter

Popular Posts

Get Updates By Email

Link

Translate