Saturday 31 August 2013

Introduction to Pointers | C Programming Language Material pdf



INTRODUCTION:

When variables are declared memory is allocated to each variable.
C provides data manipulation with addresses of variables therefore execution time is reduced. Such concept is possible with special data type called pointer. A pointer is a variable which holds the address of another variable or identifier this allows indirect access of data.

DECLARATION:
1. To differentiate ordinary variables from pointer variable, the pointer variable should proceeded by called „value at address? operator. It returns the value stored at particular address. It is also called an indirection operator( symbol * ).
2. Pointer variables must be declared with its type in the program as ordinary variables.
3. Without declaration of a pointer variable we cannot use in the program.
4. A variable can be declared as a pointer variable and it points to starting byte  address of
any data type.

SYNTAX:
Data type *pointer variable;

The declaration tells the compiler 3 things about variable p
a) The asterisk (*) tells that variable p is a pointer variable
b) P needs a memory location
c) P points to a variable of type data type.

Ex:
int * p, v;
float *x;
In the first declaration v is a variable of type integer and p is a pointer variable.
             V stores value p stores address
In the 2nd declaration x is a pointer variable of floating point data type.

2) Pointer variables are initialized by p= &v, it indicates p holds the starting address
of
integervariable v.
int v=20,*p;
p=&v; /*address of v stored in p*/
  V      p
  20    1000
Let us assume that address of v is 1000.
This address is stored in p. Now p holds address of v now *p gives the value stored at the address pointer by p i.e *p=20.

IMPORTANT POINTS:
1. A pointer variable can be assigned to another pointer variable, if both are pointing to the same data type.
2. A pointer variable can be assigned a NULL value.
3. A pointer variable cannot be multiplied or divided by a constant.
Ex: p*3 or p/3 where p is a pointer variable
4. One pointer variable can be subtracted torn another provided both winters points to Elements on same away.
5. C allows us to add integers to or subtract integers from pointers.
Ex: p1+4, p2-2, p1-p2
If both p1, p2 are pointers to same way then p2-p1 gives the number of elements between p1,p2
6. Pointer variable can be intermingled or decremented p++ & p- -
The values of a variable can be released in to way
1.by using variable name
2.by using adders.

Disadvantages:
1. Unless pointer is defined and initialized properly use of pointers may lead to disastrous situations.
2. Using pointers sometimes be confusions.
Program:
#incude < stdio.h>
main ( )
{
int x = 10, * p1, *p2, *p3;
P1=p2=p3=&x;
printf (“%d”,*p1);
printf (“% d”,*p2);
printf (“%d”,*p3);
printf (“%d”, x);
printf (“%d”,*(& x));
}

Output : 10 10 10 10 10

Void Pointers :
We can declare a pointer to be of any data type void and can assign a void pointer to any other type.
Ex : 
int x:
void p;
p= &x;
*p = 27
printf(“%d”,*p);
It given error because the pointer p is of type void and cannot hold any value.
So we have to type cast the pointer variable from one type to other type.
int x = 27;
void *p;
p=&x;
printf (“ %d”, * ((int*)p));
Output is 27

The statement (int *)p makes p to become an integer type
Ex:
#include < stdio.h>
#include < conio.h>
main()
{
int x = 27;
void *p;
p= &x;
printf (“value is = %d”, * ((int *)p));
}

Output : Value is 27


0 comments:

Post a Comment

 

C Programming Language Interview Questions and Answers Tutorial for beginners. Copyright 2013 All Rights Reserved