C - SPLessons
boy2 Chapter 23

Pointers in C

C - SPLessons

Pointers in C

shape Description

In the current chapter of Pointers in C, the introduction to address is given initially and then about the pointers and pointer variables.Any compiler need to store the variables in a memory.The memory address given to a particular variable is called as a Pointer.

Computer’s memory consists of 8 bit bytes in a continuous sequence. Portions of this memory are reserved when a program starts to store a program code, variables, functions, arguments, local variables and so on. The storage for each variable must reside in memory, and where it resides is defined by an address.

Every variable has an address even if it is not used which is just the offset from the base address to the location where the variable storage resides. Now, C provides a special category of variables specifically for handling these addresses. So these are address variables and they are called Pointers.

Pointers in C or pointer variables also has a type that indicates the variable type being pointed to provide a degree of safety as the compiler can check that the variable being pointed to, according to the rules governing that particular type. Sometimes type of a pointer might not be considered. There are many reasons to do this, but it should be done with some caution.In that situations, C provides a special type called void which means some storage whose type is unknown to the compiler.

Simply, pointer is memory locator of a variable. Pointers play a key role in exempting C-language from other programming languages like java,visual basic,etc.

Pointers and Variables

shape Description

The simplest way to start using pointers is to get the address of some variable.This variable is stored on the memory stack, and there is no need to think about where or how its storage was provided. But its address can be obtained using the address of operator.

When using Pointers in C the compiler usually unable to detect whether pointers are, in fact, pointing to some valid location. C also does not offer any run time facility for making these sorts of checks.

shape Conceptual
figure

  • In the above figure, a is the variable having value 10 located at an address 1000. This address is stored in another variable “ptr” called as pointer variable located at an address 2000.So, the datatype should be same for normal variable and pointer variable.
  • In C-language, the address of a variable can obtained by giving & in front of the variable like i=&a.

shape Syntax

pointer_type  *(pointer_name)

Eg:char *ptr;

shape Example

#include <stdio.h>
int main()
{
    int var =10;
    int *p;
    p= &var;

    printf ( "\n Address of var is: %u", &var);
    printf ( "\n Address of var is: %u", p);

    printf ( "\n Address of pointer p is: %u", &p);

    /* Note I have used %u for p's value as it should be an address*/
    printf( "\n Value of pointer p is: %u", p);

    printf ( "\n Value of var is: %d", var);
    printf ( "\n Value of var is: %d", *p);
    printf ( "\n Value of var is: %d", *( &var));
}

Output:


 Address of var is: 2358860
 Address of var is: 2358860
 Address of pointer p is: 2358848
 Value of pointer p is: 2358860
 Value of var is: 10
 Value of var is: 10
 Value of var is: 10

shape Advantages

  • Pointers helps in dynamic memory management.
  • Complexity of the program is reduced by pointers.
  • Pointers increases the efficiency while handling arrays and structures.

Null pointer

shape Description

A pointer variable will always be given a garbage value. It is better to make a pointer variable “null” in order to identify the variable easily.If a pointer is declared as null, it is called as null pointer .

Array of pointers

shape Description

Pointers in C and arrays are very closely related in C. In fact, the language construct for pointers and arrays are largely interchangeable.

Pointers are handy when dealing with arrays i.e pointer arithmetic is used to change the element being pointed to very efficiently. Pointer can be incremented and it will point to the next element in the array.


shape Example

#include<stdio.h>
#include<conio.h>
const int x=3;
int main()
{
	int a[]={10,20,30};
	int i,*ptr[x];
	for(i=0;i<x;i++)
	{
		ptr[i]=&a[i];
	}
	for(i=0;i<x;i++)
	{
		printf("value of a[%d]=%d\n",i, *ptr[i]);
	}
}

Output:

value of a[0]=10
value of a[1]=20
value of a[2]=30

Summary

shape Key Points

  • Pointers in C are variables that points to address of a another variable.
  • Specification of pointing address is must.

shape Programming
Tips

It is better to free and insert 0 in the pointer variable to avoid confusion when any new variable is allocated the address.