Search Your Topic

config
Tuesday, 13 June 2017 11:34

C Pointers

Written by
Rate this item
(0 votes)

The pointer in C language is a variable, it is also known as locator or indicator that points to an address of a value.

c pointers


Advantage of pointer

1) Pointer reduces the code and improves the performance, it is used to retrieving strings, trees etc. and used with arrays, structures and functions.

2) We can return multiple values from function using pointer.

3) It makes you able to access any memory location in the computer's memory.


Usage of pointer

There are many usage of pointers in c language.

1) Dynamic memory allocation

In c language, we can dynamically allocate memory using malloc() and calloc() functions where pointer is used.

2) Arrays, Functions and Structures

Pointers in c language are widely used in arrays, functions and structures. It reduces the code and improves the performance.


Symbols used in pointer

Symbol Name Description
& (ampersand sign) address of operator determines the address of a variable.
* (asterisk sign) indirection operator accesses the value at the address.

Address Of Operator

The address of operator '&' returns the address of a variable. But, we need to use %u to display the address of a variable.

 
  1. #include <stdio.h>      
  2. #include <conio.h>    
  3. void main(){      
  4. int number=50;    
  5. clrscr();      
  6. printf("value of number is %d, address of number is %u",number,&number);  
  7. getch();      
  8. }      

Output

value of number is 50, address of number is fff4

Declaring a pointer

The pointer in c language can be declared using * (asterisk symbol).

 
  1. int *a;//pointer to int  
  2. char *c;//pointer to char  

Pointer example

An example of using pointers printing the address and value is given below.

 

As you can see in the above figure, pointer variable stores the address of number variable i.e. fff4. The value of number variable is 50. But the address of pointer variable p is aaa3.

By the help of * (indirection operator), we can print the value of pointer variable p.

Let's see the pointer example as explained for above figure.

 
  1. #include <stdio.h>      
  2. #include <conio.h>    
  3. void main(){      
  4. int number=50;  
  5. int *p;    
  6. clrscr();  
  7. p=&number;//stores the address of number variable  
  8.       
  9. printf("Address of number variable is %x \n",&number);  
  10. printf("Address of p variable is %x \n",p);  
  11. printf("Value of p variable is %d \n",*p);  
  12.   
  13. getch();      
  14. }      

Output

Address of number variable is fff4
Address of p variable is fff4
Value of p variable is 50

NULL Pointer

A pointer that is not assigned any value but NULL is known as NULL pointer. If you don't have any address to be specified in the pointer at the time of declaration, you can assign NULL value. It will a better approach.

int *p=NULL;

In most the libraries, the value of pointer is 0 (zero).


Pointer Program to swap 2 numbers without using 3rd variable

 
  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. void main(){  
  4. int a=10,b=20,*p1=&a,*p2=&b;  
  5. clrscr();  
  6.   
  7. printf("Before swap: *p1=%d *p2=%d",*p1,*p2);  
  8. *p1=*p1+*p2;  
  9. *p2=*p1-*p2;  
  10. *p1=*p1-*p2;  
  11. printf("\nAfter swap: *p1=%d *p2=%d",*p1,*p2);  
  12.   
  13. getch();  
  14. }  

Output

Before swap: *p1=10 *p2=20
After swap: *p1=20 *p2=10
Read 148 times
Anish Sir

"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."

 Call +91 74 88 34 7779  | Email : anishsingh@live.com

toshiyas.in
More in this category: « Pointers C Pointer to Pointer »

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.