XII-B-CS (A NOTE ON--POINTER)--PART 2

new operator
Syntax: pointer variable =
new data_type;
Example:

char *cptr;
cptr = new char;
char *captr;
captr = new char[10];

delete operator
Syntax:
delete pointer variable;
Example:

delete cptr;
delete [] captr;

C++ has the concept of constant pointer and pointer to a constant. For example:

char * const cptr1="Computer Science"; //constant pointer

Here the address of cptr1 cannot be modified.

int const *iptr=&x; //pointer to a constant
const char * const cptr2="KV"; //pointer to a constant
Here the constant value, to which the pointer pointing to, can not be modified.
A NULL pointer is a pointer which indicates that it is not pointing to any valid memory address. For example:

int *iptr=NULL;

Pointer Arithmetic:

Only addition and subtraction may be performed on pointers. All the pointers increases and decreases by the length of the data type they point to. Adding 1 to a pointer adds the size of pointer‟s base type. Let iptr be an integer pointer, currently pointing to memory address 2002. If the int size is 2 bytes, then after the execution of

iptr++;

iptr will be pointing to 2004.

Arrays and Pointers:
An array name is equivalent to a pointer pointing to the first element of array. The address of the first byte is called Base Address. In C++ we may have an array of pointers also. If an array name (a pointer actually) is incremented, it points to the next element of the array. Example: What will be the output of the following program:
#include<iostream.h>
void main()
{
int Numbers[] = {2,4,8,10};
int *ptr = Numbers;
for (int C = 0; C<3; C++)
{cout<< *ptr << "@";
ptr++;
}
cout<<endl;
for(C = 0; C<4; C++)
{(*ptr)* = 2;
--ptr;
}
for(C = 0; C<4; C++)
cout<< Numbers [C]<< "#";
cout<<endl;
}
Output:
2@4@8@
4#8#16#20#

Functions and Pointers:
A function may return a reference or a pointer variable. Pointer to a function can be passed to function, returned from functions.
Structure and Pointers:
C++ allows pointers to structures like other data types and these pointers to structures are known as structure pointers.
Syntax:
struct_name *struct_pointer;
The members of the structure are accessed by using -> (arrow operator).
Syntax: struct_pointer->struct_member; Example: Find the output of the following program:
#include<iostream.h>
struct Game
{
char magic[20];
int score;
};
void main()
{
Game M={"Tiger", 500};
Char * Choice;
Choice =M.Magic;
Choice[4]=‟P‟;
Choice[2]=‟L‟;
M.Score+=5;
cout<< M.Magic<<M.Score<<endl;
Game N=M;
N.Magic[0]=‟A‟; Magic[3]=‟J‟;
N.Score-=120;
Cout<<N.magic<<N.Score<<endl;
}
Output:
TiLeP550
AiLJP430

Self Referencial Structures:

When an element of a structure is declared as a pointer to the structure itself, this type of structure is called self-referential structure. Example:

struct node
{ char data[20];
node *next;
};

The node structure contains both a data member and a pointer to the next node structure (hence, self-referential)

next of first node points to next node
next of last node = NULL
The data structure itself consists of one or more nodes, "linked" by pointers
Objects and Pointers:
C++ allows us to have pointers to objects known as object pointers.
Syntax:
class_name *object_pointer;
This Pointers:
While defining a class the space is allocated for member functions only once and separate space is allocated for each object. There exists a serious problem i.e. which object‟ data member is to be manipulated by any member function.
The "this" pointer is an already created object pointer that points to currently calling object. The this pointer is automatically passed to a member function when it is called.
For Example:

#include<iostream.h>
#include<string.h>
class employee
{ char name[20];
float salary;
public:
employee(char *n, float s)
{ strcpy(name,s);
salary=s;
}
employee greater(employee &e)
{ if(e.salary>=salary)
return &e;
else
return this;
}
void display()
{
cout<<"\nName:"<<name;
cout<<"\nSalary:"<<salary;
}
};
void main()
{employee e1("ABC",10000), e2("PQR",20000), e3("XYZ",5000);
employee *emp;
emp=e1.greater(e3);
e1->display();
emp=e2.greater(e3);
e2->display();
}

Output:

Name:ABC
Salary:10000
Name:PQR
Salray:20000

Pointers to Pointers:

We can define pointer pointing to another pointer that points to the target value.
Syntax:
int **ptr_to_ptr;
In the case of pointer to a pointer, the first pointer contains the address of the second pointer, which points to the objects that contains the value desired. Example: Give the output of the following program segment (assume all required header files are included in the program):
char *NAME= "a ProFile";
for(int x=0;x<strlen(NAME);x++)
if(islower(NAME[x]))
NAME[x]=toupper(NAME[x]);
else
if(isupper(NAME[x]))
if(x%2!=0)
NAME[x]=tolower(NAME[x-1]);
else
NAME[x]--;
Cout<<NAME<<endl;
Output:
A OROoILE
Example: Give the output of the following program segment (assume all required header files are included in the program):
char *s= "STUDY";
for(int x=0; x<strlen(s);x++)
{for(int y=0; y<=x; y++)
cout<<s[y];
cout<<endl;
}
Output:
S
ST
STU
STUD
STUDY
Example: Find the output of the following program:
#include<iostream.h>
void main()
{
int X[] = {10,25,30,55,110};
int *p = X;
while(*p< 110)
{
if (*p%3!=0)
*p =*p + 1;
else
*p =*p + 2;
p++;
}
for(int i=4;i>=1;i--)
{ cout<<X[i]<<"@";
if (i%3==0) cout<<endl;
}
cout<<X[0]*3<<endl;
}
Output:
110@56@
32@26@33
************************************** THE END ************************************

Popular posts from this blog

XII- CS-- SOLVED SQL queries (SQL PRACTICAL ASSIGNMENS)

SQL--HOME ASSIGNMENTS(XII Class)

Python-MySQL Connectivity