STRUCTURES
AND POINTERS
We know
that all data is not of fundamental /basic types.rather many of them may be composed of elementary
data items.No programming language can provide data types of all kinds of
data.So programming languages provide facility to define new data types,as
desired by the users.a user defined datatype named STRUCTURE .
STRUCTURE
Structure is a user
defined data type of c++ to represent a collection of logically related data
items,which may be of different types,under a common name.
Syntax:
Struct
tagname
{
datatype variablename1;
datatype variablename2;
……………………………………..
datatype variablename n;
};
The variable specified within the pair of braces are known as elements or
members.
Example :
Struct student
{
int rollno;
char name[25];
int age;
float fee;
};
VARIABLE DECLARATION
AND MEMMORY ALLOCATION
Syntax:
1)
Struct tagname
{
datatype variablename1;
datatype variablename2;
……………………………………..
datatype variablename n;
} variable 1, var 2,…..,var n ;
Eg:
Struct student
{
int rollno;
char name[25];
int age;
float fee;
} st1,st2,st3;
Or
syntax:
struct tagname var1,var2,………..,var n;
Eg:
Struct
student
{
int rollno;
char name[25];
int age;
float fee;
} ;
…………………………………..;
…………………………………..;
student st1,st2,st3;
DYNAMIC STRUCTURE VARIABLE
St1 =new
student;
POINTER STRUCTURE
VARIABLE
STUDENT
* ST1;
OR
struct student
{
int age ;
…………………
} *st1;
ACCESSING OF
STRUCTURE POINTER VARIABLE (using arrow operator ->)
St1 -> age=20;
Referencing structure elements:
Structure
elements are referenced by dot ( . ) operator.
Eg
:
structure member ‘rollno’ in structure
‘address’ is referenced as a.rollno.
Syntax :
Structure variable. Structure
member
Array
of structures:
Structures
in the form of arrays is called array of structures
Eg
: address a[10]; creates 10 structures as a array.
First
structure variable will be a[0].
Members
are referenced as a[0].rollno & a[0].name so on.
Nested
structure:
Structure with in another structure is called nested structure.
1)
2)
struct
dob
{
int dd;
struct address
{
int rollno;
dob db;
};
};
Array vs structure
POINTER
The data stored in computer
occupies a memory cell.Each cell has a unique address.A pointer points to the
address of memory location.It holds the address of the memory location.
Declaring a pointer
We can declare a
pointer,similar to a variable.The syntax is
Data_Type *Pointer_Variable;
For Example:
int *ptr;
Here ptr is a pointer variable
and points to an integer data type. Initializing a pointer
We can initialize a pointer as,
Ptr=&a;
Here the pointer variable ptr
contains the address of variable a.
The content
of or deferencing operator or indirectionoperator ( * ) is used
to get the value stored at the location held by a pointer variable.The *
operator is also known as value at operator. The address of operator(&)
is used to retrieve the address of a variable.
#include<iostream>
void main()
{
int a=10;
int *Ptr=&a;
cout<<"The value of
a="<<a<<"\n";
cout<<"The address
of a="<<Ptr;
}
Output:
The value of a=10
The address
of a= oxffoo;
Memory allocation
Allocation of memory for a
variable during compilation time is known as static memory allocation.Once the
memory is allocated during compile time it cannot be expanded or compressed.
For example:
int a[10] as integer;
During compilation the compiler
will allocates 20 bytes .
The process
of allocating memory during execution time is called dynamic memory
allocation.This is done using ‘new’ and ‘delete’ operators.
new and delete operators
The new operator
is used to allocate memory during execution(run-time).It is a unary
operator.The syntax is
pointer_variable=new
data_type;
eg:
ptr = new int;
The delete operator
is used to free memory allocated using new operator.The syntax is
delete
pointer_variable;
eg:
delete ptr;
Memork
leak
Memork leak is
a situation where once memory is dynamically allocated to a variable and if
later the memory is not de-allocated after its purpose is satisfied,the memory
gets lost.This problem where memory is dynamically allocated but not released
,hence not accessible to any problem is called memory leak.
Pointers and array
There is a close relationship
between pointers and array.C++ treats the name of array as a pointer.The name
of array is a pointer pointing to the first element of the array.
For example:
char ch[10],* ptr;
ptr=&ch;
here ptr points to the address
of first array element in ch.To access fifth element you can use ch[4] ; or
*(ptr+4);
Note:
Currently ptr points to address of first element in the array ch.
OPERATIONS ON POINTERS
Arithmetic operations can be
performed on a pointer.
For Example;
int a[50];
int *ptr;
ptr=&a[0] ; //ptr refers to
the base address of array a.
(same as ptr =a ;in case of pointer array)
DYNAMIC ARRAY
A dynamic array is an array
created during run time using new operator.The synatx for creating a dynamic
array is
Pointer= new data_type[size];
For example:
P=new int[10];
(declares a dynamic array of size 10.)
INITIALIZATION
OF DYNAMIC VARIABLE
P =
new int (10) ;
(value
10 stored to dynamic variable p.)
POINTER
AND STRING
A string is an array of
characters,and array name can be considered as a string variable.Every string
in C++ is terminated by a Null character(\0).For example the string hello
is represented in memory as Intializing an array .
The above array can be
initialized as
char ch[ ]=”hello”;
or
char
ch={‘h’,’e’,’l’,’l’,’o’,’\0’};
We can use a character
pointer to reference the array.
char s[10];//character array
declaration
char *ptr;//character pointer
declaration;
cin>>s;
ptr=s;//Copying
contents of s to ptr
Advantages of character
pointer
Strings can be managed by optimal memory space.
Assignment operator can be used to copy
strings.
No wastage of memory.
SELF REFERENTIAL
STRUCTURES
When a member of a structure is
declared as a pointer to the structure itself,then the structure is called as
self referential structure.
For example:
struct chain
{
int val;
chain *p;
};



No comments:
Post a Comment