Showing posts with label C Pointers. Show all posts
Showing posts with label C Pointers. Show all posts

Wednesday, July 4, 2018

C Pointers

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

c pointers

  • Pointers in C language is a variable that stores/points the address of another variable. A Pointer in C is used to allocate memory dynamically i.e. at run time. The pointer variable might be belonging to any of the data type such as int, float, char, double, short etc.
  • Pointer Syntax : data_type *var_name; Example : int *p; char *p;
  • Where, * is used to denote that “p” is pointer variable and not a normal variable.

KEY POINTS TO REMEMBER ABOUT POINTERS IN C:

  • Normal variable stores the value whereas pointer variable stores the address of the variable.
  • The content of the C pointer always be a whole number i.e. address.
  • Always C pointer is initialized to null, i.e. int *p = null.
  • The value of null pointer is 0.
  • & symbol is used to get the address of the variable.
  • * symbol is used to get the value of the variable that the pointer is pointing to.
  • If a pointer in C is assigned to NULL, it means it is pointing to nothing.
  • Two pointers can be subtracted to know how many elements are available between these two pointers.
  • But, Pointer addition, multiplication, division are not allowed.
  • The size of any pointer is 2 byte (for 16 bit compiler).

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

SymbolNameDescription
& (ampersand sign)address of operatordetermines the address of a variable.
* (asterisk sign)indirection operatoraccesses 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.





#include<stdio.h>
int main(){
int number=50;
printf("value of number is %d, address of number is %u",number,&number);
return 0;
}



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).

int *a;//pointer to int

char *c;//pointer to char

Pointer example

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

pointer example

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.

pointer variable stores the address of number variable

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.





#include<stdio.h>
int main(){
int number=50;
int *p;
p=&number;//stores the address of number variable
printf("Address of p variable is %x \n",p);
printf("Value of p variable is %d \n",*p);
return 0;
}



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





#include<stdio.h>
int main(){
int a=10,b=20,*p1=&a,*p2=&b;

printf("Before swap: *p1=%d *p2=%d",*p1,*p2);
*p1=*p1+*p2;
*p2=*p1-*p2;
*p1=*p1-*p2;
printf("\nAfter swap: *p1=%d *p2=%d",*p1,*p2);
return 0;
}



Output

Before swap: *p1=10 *p2=20

After swap: *p1=20 *p2=10

Another example program for pointers





#include <stdio.h>
int main()
{
int *ptr, q;
q = 50;
/* address of q is assigned to ptr */
ptr = &q;
/* display q's value using ptr variable */
printf("%d", *ptr);
return 0;
}



output

50




Instagram