Thursday, 23 March 2017

C++ JAVA .NET in Govindpuram, Ghaziabad

Pointers in C++ ( CBSE-XII)

Introduction to Pointer, Declaration and Initialization of Pointer; Dynamic memory allocation/deallocation
operators: new, delete; Pointers and Arrays: Array of Pointers, Pointer to an array (1 dimensional
array), Function returning a pointer, Reference variables and use of alias; Function call by reference. Pointer
to structure: De-reference/Deference operator: *, ->; self referencial structure;

6.1 Introduction
The C and the C++ languages are still existing popular programming languages in the computer world. The major reason behind this is pointers.  Pointers help to refer the memory locations to store and access the data into computer memory. Pointers are also used to access the more than one memory locations sequentially. Pointers also help to allocate memory locations dynamically.
Pointer is a special type variable, which does not store the values, rather it store the address of another variable.    This concept might sound the unnecessarily complicated, but it implies a number of advantages like for instance more efficient programming code, faster execution of program and saving of memory. Object oriented programming such as C++ always gets these advantages when copying objects or sending objects to functions.
The concept of pointer is the unique concept in the C++ programming language, which is not available in the latest languages Java and Visual Basic. Therefore, C++ is still growing the in the computer era since 1980.  
This chapter will explain the basic concepts of pointers and their use. You will learn use of pointers with different data types. The method of declaring and assigning the values to the pointer are elaborated in the student’s point of view. This chapter also examine the analogy between pointers and arrays and how to use pointers as parameters for the functions.
Additionally, this chapter will explain the concept of dynamic memory allocation, which actually does not closely relate to pointers, but still often is used in connection with pointers.
6.2 Definition
A pointer is a variable to store the address of another variable. This address is also refer as a memory location of the variable, which is the address of primary memory.
In other words, a variable which is already declared in the memory may or may not contain the value, the pointer variable can store the address of this variable. This variable may be of any data type can have pointer variable to store their addresses. Let’s see an example in the figure:
      int number = 10;
The above statement shows the number is an integer variable has the value 10. The primary memory will allocate the memory for this variable like that:
10
number →           10011
  
The description of above variable is:
Name of variable                                                                :               number
Address of variable in primary memory         :               10011
Value of the variable is                                      :               10
For above variable if we defined a pointer variable it will looks like in the memory like as given in the figure:
10011
Pnumber →        10013

The description of above pointer variable is:
Name of pointer variable                                                   :               Pnumber
Address of pointer variable in primary memory           :               10013
Value of the pointer variable is                                         :               10011
Using this concept we may access the value of number variable using pointer variable Pnumber, which is described in later section of this chapter.
6.3 Declaration and Initialization of Pointer
To declare the pointer variable that can point to an integer value, you can use the following syntax:
      int* Pnumber;
The asterisk (*) symbol as postfix on the type says that the variable being defined is a pointer to an object of that type: Pnumber can point to a place in memory that can contain a value of type int.    
You must always specify the data type pointed to by the pointer variable. Below we declare to a double value:
      double* pSize;
Below is the example of declaring a pointer to a char value:
      char* pChr;
You can as well place the space in front of the asterisks. The declarations above could be written:
      int *Pnumber;
      double *pSize;
      char *pChr;
Both variants of pointer declarations are allowed in C++.
To assign the address of any variable into the pointer variable we need to use the & operator. Let see given example:
                int number = 34;
      int *pNumber = &number;
Here a pointer variable pNumber is created and assigned the address of variable number. The variable number and the pointer variable pNumber now points to the same memory location, which means value 23. 
You can’t directly assign a fixed value to the pointer in the declaration:
      int *pNumber = 34;      //wrong assignment
since currently there is no specific memory locational pointed to by pNumber. However, when pNumber has got its memory address, we can change the value in the location indicated by pNumber:
*pNumber = 25;
Here we must remember to use asterisk together with the name of the pointer variable. The program then understand that it is the value that is to be changed.
Compare this to this erroneous statement:
pNumber = 25;     //wrong statement
This would mean that we updated the address pointed to by pNumber. The address 25 would be pointed to, which of course is erroneous.
In the concept of pointers two operators are generally used :
Operator
Meaning
*
the content of
&
the address to
 To understand the use these operators, let see an example:
Example 6.1: Program to show the use of * and & operators.
# include < iostream.h >
void main()
{
int num = 10;
      int *p = &num;

      cout << “\n num :” << num << endl;
      cout << “\n &num :” << &num << endl;
      cout << “\n *(&num) :” << *(&num) << endl;
      cout << “\n p :” << p << endl;
      cout << “\n &p :” << &p << endl;
cout << “\n *(&p) :” << *(&p) << endl;
cout << “\n *p :” << *p << endl;
}

OUTPUT
num   :     10                // content of num
&num : <address of num>      // address of num
*(&num) : 10                  // content of address of num
p     : <address of num>      // content of p
&p    : <address of p>        // address of p
*(&p) : <address of num>      // content of address of p
*p    : 10        // content of address stored at p means content of num

Now have a look at how pointers work in connection with string variables, i.e. arrays of char type.
Example 6.2: Program to use the pointers with string.  
#include < iostream.h >
void main()
{
char name[] = ”Raj Kumar”;
char* pname = name;

      cout << name << endl;
cout << *pname << endl;
      cout << pname << endl;
     
pname++;
      cout << pname << endl;
     
pname+=5;
      cout << pname << endl;
}
OUTPUT
Raj Kumar
R
Raj Kumar
aj Kumar
mar

In the above example, output is explained below statement by statement:

cout << name << endl;                         :Statement prints the content of variable name,
cout << *pname << endl;       :Statement prints the content of first element of array,

cout << pname << endl;                       : which should actually print the address stored in pname, but cout performs a reinterpretation . it takes the content in the memory locations pointed to by pname, i.e. the character ‘R’, and prints character by character until the null character is found. This means, it printed ‘Raj Kumar’ which is entire string.

pname++;                       : This statement increase the pointing location of pointer variable pname, now it is pointing at the second element of array, i.e. the character ‘a’.
 cout << pname << endl;                      : after increasing the location of pointer, it printed ‘aj Kumar’ which is entire string from second location.

pname+=5;                                                          : This statement will increase the 5 more pointing location of pointer variable from current location. Therefore, it printed ‘mar’ which is entire string from second location.

Using pointer the value of ordinary variable may be updated directly. Let see another example, where the ordinary variables are updated using pointers automatically.

Example 6.3 : Program to update the variables using pointers.

#include < iostream.h >
void main()
{
int a = 10;
int* ptr = &a;
      cout << “\n Value of a = ” << a;

      *ptr = *ptr + 10;
      cout << “\n Value of a = ” << a;

      *ptr = 300;
      cout << “\n Value of a = ” << a;

      *ptr = a / 2;
      cout << “\n Value of a = ” << a;

}
OUTPUT
Value of a = 10
Value of a = 20
Value of a = 300
Value of a = 150

Using pointer the values of more than ordinary variables may be stored separately. Let see another example, where the ordinary variables are stored using pointers and displayed accordingly.
ptr
a
b
c
 










Example 6.4 : Program to store address of many variables using pointers.

#include < iostream.h >
void main()
{
int a = 10, b = 20, c = 30;
int* ptr;

ptr = &a;
      cout << “\n Value of a = ” << *ptr;

ptr = &b;
      cout << “\n Value of b = ” << *ptr;

ptr = &c;
      cout << “\n Value of c = ” << *ptr;
}

OUTPUT
Value of a = 10
Value of b = 20
Value of c = 30

Using multiple pointers the address of any single ordinary variable may be stored. Let see another example, where the only one ordinary variable is stored in multiple pointers and displayed accordingly.
a
P11
P2
P3
 











Example 6.4 : Program to store address of one variable into multiple pointers.

#include < iostream.h >
void main()
{
int a = 10;
int *p1, *p2, *p3;

p1 = &a;
p2 = &a;
p3 = &a;
     
cout << “\n p1, p2 and p3 pointing on same address:” << &a;
cout << “\n Value of a using pointer p1 = ” << *p1;
cout << “\n Value of a using pointer p2 = ” << *p2;
cout << “\n Value of a using pointer p3 = ” << *p3;

a = 20;
cout << “\n Value of a after change :” << a;
cout << “\n Value of a using pointer p1 = ” << *p1;
cout << “\n Value of a using pointer p2 = ” << *p2;
cout << “\n Value of a using pointer p3 = ” << *p3;

*p1 = *p1+10;
cout << “\n Value of a after change :” << a;
cout << “\n Value of a using pointer p1 = ” << *p1;
cout << “\n Value of a using pointer p2 = ” << *p2;
cout << “\n Value of a using pointer p3 = ” << *p3;

}

OUTPUT
p1, p2 and p3 pointing on same address: 1104
Value of a using pointer p1 = 10
Value of a using pointer p2 = 10
Value of a using pointer p3 = 10
Value of a after change = 20
Value of a using pointer p1 = 20
Value of a using pointer p2 = 20
Value of a using pointer p3 = 20
Value of a after change = 30
Value of a using pointer p1 = 30
Value of a using pointer p2 = 30
Value of a using pointer p3 = 30


6.5 Dynamic memory allocation/de-allocation
In the C++ programming language, there are two ways that memory gets allocated for data storage:
1.       Static Memory Allocation
The memory for defined variables is allocated by the compiler itself. The exact size and data type of variable to store must be known at the time of compilation. Therefore, standard array declarations need the constant size.
2.       Dynamic Memory Allocation
The memory allocated for the variables during the run time. The dynamically allocated space usually placed in a program segment known as the heap or the free store. In this allocation exact amount of space or number of items does not have to be known by the compiler in advance. Therefore, for dynamic memory allocation, pointers are crucial.

We can dynamically allocate storage space while the program is running, but we cannot create new variable names during the execution.
Therefore to implement the concept of dynamic allocation, it requires two steps:
1.       Creating the dynamic space.
2.       Storing its address in a pointer (so that the space can be accessed)
In the C++, new operator is used to create the dynamic memory space.
De-allocation is the process to clean-up the space being used for variables or other data storage. The compile time variables are automatically de-allocated based on their known extent (this is the same as scope for "automatic" variables). It is the programmer's job to de-allocate dynamically created space, to de-allocate dynamic memory space the delete operator is available in C++ programming language.

6.5.1 new and delete operators
In the C++, for allocation and de-allocation of memory dynamically,  new and delete operators are used.
To allocate space dynamically, use the unary operator new, followed by the type being allocated.

 new int;        // dynamically allocates an int
 new double;     // dynamically allocates a double

These above statements are not very useful by themselves, because the allocated spaces have no names, and the new operator returns the starting address of the allocated space, and this address can be stored in a pointer:

 int * p;        // declare a pointer p
 p = new int;    // dynamically allocate an int and load address into p
 double * d;     // declare a pointer d
 d = new double; // dynamically allocate a double and load address into d

Consider the following example for dynamically allocation and de-allocation of the variables.

Example 6.4 : Program to allocate and de-allocate the dynamic space using pointers.

#include < iostream.h >
void main()
{
int *p1;

p1 = new int;     // dynamic memory allocation
*p1 = 300;

cout << “\n Value inside the p1 = ” << *p1;
     
delete p1;        //free the dynamically allocated memorys
}
OUTPUT
Value inside the p1 = 300

Using the new and delete operators, we can create arrays at runtime by dynamic memory allocation and de-allocation. The general form for doing this is:

p_var = new array_type[size];

size specifies the number of elements in the array.

To free the memory of an array we use:

delete[] p_var;


Let see the given example for the above explanation.

Example 6.5 : Program to create dynamic array using pointers.

#include < iostream.h >
void main()
{
int *p;

p = new int[10];  // dynamic allocation for array of 10 elements

for(int i = 0; i < 10; i++)
p[i] = i;

cout << “\n content of array are : ” << endl;

for(i = 0; i < 10; i++)
cout << p[i] << endl;
     
delete[] p;       //free the array memories
}

OUTPUT
content of array are :
0
1
2
3
4
5
6
7
8
9

6.6 Pointers and Arrays
As we know that for an array, compiler declares the memory in contiguous memory locations. The base address of array points on the first element of the array. The array always has a constant name for accessing the elements. The pointer helps to arrays with following features and advantages:  
Advantages of pointers over arrays:
-         Using pointers we can save the memory which are unused by the declaring of arrays.
-         Using pointers we can access the memory locations of arrays easily.
-         Using pointers we can execute the program fast.
6.7 Pointers to an Array
Suppose an array is declared like that:

int arr[5] = {1, 2, 3, 4, 5};

Suppose the base address of arr is 1000 and assuming that each integer requires two bytes, the five elements will be stored as following format:

Index   
arr[0]
arr[1]
arr[2]
arr[3]
arr[4]
Values  
1
2
3
4
5
Address 
1000
1002
1004
1006
1008
  
The name arr is defined as a constant pointer pointing to the first element, arr[0] and therefore the value of arr is 1000, the location where arr[0] is stored. That is:
               
                arr  = &arr[0] = 1000

If we declare p as an integer pointer, then we can make the pointer p to point to the array arr by the following assignment:

                p = arr;

This  is also equivalent to

                p = &arr[0];

Now, we can access every value of arr using p++ to move from one element to next element. The relationship between pointer variable p and array arr is shown as:

      p = &arr[0];            // means base address 1000
      p+1 = &arr[1];          // means address 1002
      p+2 = &arr[2];          // means address 1004
      p+3 = &arr[3];          // means address 1006
      p+4 = &arr[4];          // means address 1008

You may notice that the address of an element is calculated using its index and the scale factor of the data type. For instance:

      Address of arr[3] = base address  + ( 3 × scale factor of int)
                  = 1000 + (3 × 2)
                  = 1006

When handling arrays, instead of using array indexing, we can use pointers to access array elements. Note that *(p+3) gives the value of arr[3]. The pointer accessing method is much faster than array indexing.

Let see an example that illustrate the use of pointer accessing method.

Example 6.6 : Program to access the array elements using pointers.

#include < iostream.h >
void main()
{
int *p, i = 0;
int arr[5] = {1, 2, 3, 4, 5};

p = arr;

cout << “\n content of array are : ” << endl;

while(i < 5)
{
cout << “\n arr[” << i++ << ”] = ” << *p;
            p++;
}
}

OUTPUT
content of array are :
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5

Let review another example that also produce same output.

Example 6.7 : Program to access the array elements using pointers.

#include < iostream.h >
void main()
{
int *p, i = 0;
int arr[5] = {1, 2, 3, 4, 5};

p = arr;

cout << “\n content of array are : ” << endl;
while(i < 5)
{
cout << “\n arr[” << i << ”] = ” << *(p + i);
i++;
}
}

OUTPUT
content of array are :
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5

As you saw in the example 6.2 about the string with pointers, actually strings are treated like character arrays and therefore they are declared and initialized as follows:

                                char st[7] = “Welcome” ;

in above statement compiler automatically adds the null character ‘\0’ at the end of the string. Pointers  provides an alternate method to create the string.

char *st = “Welcome”;

This create a string for the literal and then stores its address in the variable. The pointer variable now points to the first character of the string “Welcome” as:

               
W
e
l
c
o
m
e
\0


      st   

We can also use the run-time assignement for giving values to a string pointer. Example

                                char *st;
            st = “Welcome”;

Note that the assignment
                               
st = “Welcome”;

is not a string copy, because the variable st is pointer, not a string.  we can print the content of string st using cout as follows:
                               
cout << st;

using pointer we can find out the length of string using the pointer and their address concept. Let see the example given below:
Example 6.7 : Program to find out the length of string using pointers.

#include < iostream.h >
void main()
{
int len = 0;
int *ch, *str;

str = ”INDIA”;
ch = str;

while(*ch != ‘\0’)
{
      cout << *ch << endl;
      ch++;
}

len = ch – str;
cout << “\n Length of the string is = ” << len;
}

OUTPUT
I
N
D
I
A
Length of the string is = 5


6.8 Array of pointers
Like a simple array, pointers may be defined in the form of arrays. consider the following array of strings:

      char name[4][20];

This declaration says that the name is a double dimensional array, may contain 4 strings with maximum length of 20 characters (including null character). The total storage requirements for the name array are 80 bytes. But we know that rarely all strings will be equal length. Therefore, we may use the pointers instead of making fixed size for each string. For example, we may declare array of pointers:

char *name[4] = {
                  “Raj Kumar”,
                  “Neha Rani”,
                  “Rakesh”,
                  “Sonu”
                  };

Above declaration has array of pointers, where name to be an array of four pointers to characters, each pointer pointing to a particular name as:

      name[0]     à    Raj Kumar
      name[1]     à    Neha Rani
      name[2]     à    Rakesh
      name[3]     à    Sonu

This declaration allocates only 32 bytes, which is sufficient to hold all the characters as shown below:

R
a
j

K
u
m
a
r
\0
N
e
h
a

R
a
n
i
\0
R
a
k
e
s
h
\0



S
o
n
u
\0






The following statement would print out all the three names:
               
                for(int i = 0; i <= 2; i++)
            cout << name[i];

To access the j-th character in the i-th name, we may write as:

                *(name[i]=j)

The character arrays with the row of varying length are called ragged array and are better handled by pointers.

6.8 Function and Pointers

Pointers also support to pass the arguments to functions, when we pass the addresses to a function, the parameters receiving addresses should be pointers. The process of calling a function using pointers to pass the addresses of variables is known as call by reference and the process of passing the actul value of variables is known as call by value. The function which is called by reference can change the value of the variable used in the call. Let see an example of ‘call by value’ and ‘call by reference’:

Example 6.8 : Program to show the call by value and call by reference.

#include < iostream.h >

void change( int x );
void p_change( int *y);

void main()
{
int A = 10;
cout << “\n value of A : ” << A;

change(A);
cout << “\n value of A after calling function change : ” << A;

p_change(&A);
cout << “\n value of A after calling function p_change : ” << A;
}

void change ( int x )         // function call by value
{
      x = x + 10;
}

void p_change ( int *y )      // function call by reference
{
      *y = *y + 20;
}

OUTPUT
value of A : 10
value of A after calling function change : 10
value of A after calling function p_change : 30

Above example clearly shows the difference between call by value and call by reference. The function change(int x) shows the use of call by value and p_change(int *y) shows the use of call by reference.

When function change() is called it passes the value of variable A, which copied to the function parameter x and inside the function value has been updated, which is not effect on the actual argument A in the main. On the other hand when function p_chage() is called it passes the address of A instead of value, which copied to the pointer variable y, and inside the function content of pointer variable has been updated, which directly updated the actual argument A in the main.    

Using this concept we can swap the values of two variables, which also not possible in call by value. Let see another example:

Example 6.8 : Program to swap the values of two variables using pointers.

#include < iostream.h >

void interchange( int x, int y );
void swap( int *x, int *y);

void main()
{
int A = 10, B = 15;

cout << “\n value of A : ” << A;
cout << “\n value of B : ” << B;

interchange(A,B);
cout << “\n After calling interchange function ”;
cout << “\n value of A : ” << A;
cout << “\n value of B : ” << B;

swap(&A,&B);
cout << “\n After calling swap function ”;
cout << “\n value of A : ” << A;
cout << “\n value of B : ” << B;
}

void interchange ( int x, int y )         // function call by value
{
      int temp;
temp = x;
x = y;
x = temp;
}

void swap ( int *x, int *y ) // function call by reference
{
int temp;
temp = *x;
*x = *y;
*x = temp;
}

OUTPUT
value of A : 10
value of B : 15

After calling interchange function
value of A : 10
value of B : 15

After calling swap function
value of A : 15
value of B : 10

6.9 Function returning Pointers
We have seen in earlier examples that a function can return a single value by its name or return multiple values through pointer parameters. Since pointers are a data type in C++, we can also force a function to return a pointer to the calling function. Let see an example for the same.

Example 6.9 : Program to return pointers by functions.

#include < iostream.h >
int *max( int *x, int *y );
int *min( int *x, int *y);
void main()
{
int A = 10, B = 15;
int *p;

p = max(&A, &B);
cout << “\n Big value is : ” << *p;

p = min(&A, &B);
cout << “\n Small value is : ” << *p;
}

int *max ( int *x, int *y )   // function call by reference
{
      if( *x > *y )
            return( x );
      else
            return( y );
}

int *min ( int *x, int *y )   // function call by reference
{
      if( *x < *y )
            return( x );
      else
            return( y );
}

OUTPUT
Big value is: 15
Small value is: 10
The function max()and min() receive the addresses of variable A and B, decides which one is larger and smaller using pointers x and y and then returns the address of its location respectively. The returned address is then assigned to the pointer variable p in the calling function.  
6.10 Reference Variables and Alias

Like a pointer variables C++ also supports reference variables which works same like pointer variables only the little bit syntaxes are change. Let see an example for the same.

Example 6.10 : Program to show the use of reference variables.

#include < iostream.h >
void update( int &x );
void main()
{
int A = 10;
cout << “\n original value of A : ” << A;

update(A);
cout << “\n value of A after calling update function : ” << A;

int &b=A;         //declaration of reference variable
b=1000;

cout << "\n value of A after updating reference variable : " << A;
}
void update ( int &x)   //function with reference variable
{
      x = 100;
}

OUTPUT
original value of a : 10
value of a after calling update function : 100
value of A after updating reference variable : 1000
The function update()receive the value of variable A into the reference variable  x, and updated the value of x which directly effects on the original value of variable A. In the second case an integer reference variable &b is created and updated which also effects on the original value of A.
Reference variable as an Alias
A reference variable is also used as an alias, that is, another name for an already existing variable. Once a reference is initialized with a variable, either the variable name or the reference name may be used to refer to the variable.
Reference variables are often confused with pointers but there are major differences between reference variables and pointer variables are:

Reference variable
Pointer variable
·         Reference variable don’t have NULL value.
·         Pointer variable may has the NULL value.
·         Once a reference variable is initialized to an object, it cannot be changed to refer to another object.
·         Pointers variable can be pointed to another object at any time.

·         A reference variable must be initialized when it is created.
·         Pointer variable can be initialized at any time.

Let see another example for the use as alias.

Example 6.10 : Program to show the use of reference variables as alias.

#include < iostream.h >
void main()
{
int A ;
float B;

//declaration of reference variables for A and B
int &X = A;
float &Y = B;

A = 10;
cout << “\n value of A : ” << A;
cout << “\n value of reference A : ” << X;

B = 11.37;
cout << “\n value of B : ” << B;
cout << “\n value of reference B : ” << Y;

//Updating the reference variables
X = X + 1;
Y = Y + 1.0;
cout << “\n Reference variables are updated.”;
cout << “\n value of A : ” << A;
cout << “\n value of B : ” << B;
}
OUTPUT
value of A : 10
value of reference A : 10
value of B : 11.37
value of reference B : 11.37
Reference variables are updated.
value of A : 11
value of B : 12.37
6.10 Structure and Pointers

You can declare pointers to structures in the same way as declaring pointers to other data types. Let see an example:

struct student
{
      char name[20];
      int clas;
      float marks;
};

student stud;
student *st;

The address of structure variable can be initialized similar to the normal way:

st = &stud;
The advantage is to be able to use pointer arithmetic to step through the different structure variables:

st++;
This statement moves to the next structure variable.
With a pointer to a structure you are not allowed to use the dot (.)operator as delimiter between the variable and member. You must use the (->) characters. The symbol -> is called the arrow operator ( also known as dereference operator) and is made up of minus sign and a greater than sign.   Let see a program example for the use of structure to pointer:

Example 6.10 : Program to show the use of structure and pointers.

#include < iostream.h >
struct student
{
      char name[20];
      int clas;
      float marks;
};

void main()
{
      student stud[3];
      student *st;
      for( st = stud; st < stud + 3; st++)
      {
            cout << “\n enter name of student :”;
            cin >> st -> name;
            cout << “\n enter class of student :”;
            cin >> st -> clas;
            cout << “\n enter total marks of student :”;
            cin >> st -> marks;
      }
      st = stud;
      cout << “\n Students Records are:”;
      while(st < stud + 3)
      {
            cout << “\n Name : ” << st->name;
            cout << “\n class : ” << st->clas;
            cout << “\n Total Marks : ” << st->marks;
            st++;
      }
}
OUTPUT
enter name of student : Arush
enter class of student : 2
enter total marks of student : 450

enter name of student : Dev
enter class of student : 3
enter total marks of student : 355

Students Records are:

Name : Arush
class : 2
Total Marks : 450

Name : Dev
class : 3
Total Marks : 355

In the above example, when the pointer st is incremented by one, it is made to point to the next record. i.e. stud[1]. The statement give below may be use alternatively:

                st->name;

We could use the notation

      (*st).name;

to access the member name. The parentheses around *st are necessary because the member operator (.) has higher precedence than the operator (*).
  
6.11 Self-referential Structure
The structure may have another structure, which known as sub structure. But like a recursion any structure refer itself by the use of pointers. A self-referential structure is used to create data structures like linked lists, stacks, etc. Following is an example of this kind of structure:

struct struct_name
{
datatype datatypename;
struct_name * pointer_name;
};

A self-referential structure is one of the data structures which refer to the pointer to (points) to another structure of the same type. For example, a linked list is supposed to be a self-referential data structure. The next node of a node is being pointed, which is of the same struct type. For example,

struct linklistnode
{
int data;
struct linklistnode *next;
};

In the above example, the linklistnode is a self-referential structure – because the *next is of the type struct linklistnode. The details of linklist and stacks are define in later chapters.



- CBSE
- CBSE Curriculum, CBSE study material, CBSE XI sample papers, CBSE XI previous year question papers
- CBSE textbooks , CBSE books download, CBSE XII sample papers, CBSE XII previous year question papers,
- CBSE IX sample papers , CBSE X sample papers , CBSE X previous year papers, CBSE syllabus, CBSE classes 
- CBSE Computer Sc Question papers, CBSE model papers, CBSE coaching in Govindpuram, Ghaziabad. 
- C++ Classes, JAVA Classes,  C#.NET classes
- C++ in Govindpuram, C++ Coaching in Govindpuram, C++ Tuition in Govindpuram, C++ Classes in Govindpuram
- JAVA in Govindpuram, JAVA Coaching in Govindpuram, JAVA Tuition in Govindpuram, JAVA Classes in Govindpuram
- C#.NET in Govindpuram, C#.NET Coaching in Govindpuram, C#.NET Tuition in Govindpuram, C#.NET Classes in Govindpuram
- English Speaking in Govindpuram, English Speaking Coaching in Govindpuram, English Speaking Tuition in Govindpuram, English Speaking Classes in Govindpuram
- Spoken English in Govindpuram, Spoken English Coaching in Govindpuram, Spoken English Tuition in Govindpuram, Spoken English Classes in Govindpuram
- C++ in Ghaziabad, C++ Coaching in Ghaziabad, C++  Tuition in Ghaziabad, C++ Classes in Ghaziabad
- JAVA in Ghaziabad, JAVA Coaching in Ghaziabad,  JAVA Tuition in Ghaziabad, JAVA Classes in Ghaziabad
- C#.NET in Ghaziabad, C#.NET Coaching in  Ghaziabad, C#.NET Tuition in Ghaziabad, C#.NET Classes in Ghaziabad
- English Speaking in Ghaziabad, English Speaking Coaching in Ghaziabad, English Speaking Tuition in Ghaziabad, English Speaking Classes in Ghaziabad
- Spoken English in Ghaziabad, Spoken English Coaching in Ghaziabad, Spoken English Tuition in Ghaziabad, Spoken English Classes in Ghaziabad
- C++ training in Govindpuram, C++ institute in Govindpuram, C++ jobs in Govindpuram, C++ center in Govindpuram
- C++ training in Ghaziabad, C++ institute in Ghaziabad, C++ jobs in Ghaziabad, C++ center in Ghaziabad
- JAVA training in Govindpuram, JAVA institute in Govindpuram, JAVA jobs in Govindpuram, JAVA center in Govindpuram
- JAVA training in Ghaziabad, JAVA institute in Ghaziabad, JAVA jobs in Ghaziabad, JAVA center in Ghaziabad
- C#.NET training in Govindpuram, C#.NET institute in Govindpuram, C#.NET jobs in Govindpuram, C#.NET center in Govindpuram
- C#.NET training in Ghaziabad, C#.NET institute in Ghaziabad, C#.NET jobs in Ghaziabad, C#.NET center in Ghaziabad
- english speaking course in ghaziabad, english speaking course in ghaziabad,english speaking course in ghaziabad, utter pradesh,english speaking course in ghaziabad,english speaking course in ghaziabad,