Plus Two Computer Science Notes Chapter 1 Structures and Pointers

Kerala Plus Two Computer Science Notes Chapter 1 Structures and Pointers

a) Structure is a group of different types of logically related data referenced by a single name.
Eg.

  • The collection of details of a student that may contain various data with different data types.
Data Data Type
Student Id Integer
Name String
Data of Birth Date(int-int-int
Sex Character

b) General Form of the declaration is
struct structure tag

{
Data members;
} structure variables;
Or
structure_tag var1,var2,....
Eg:
struct date
{
int dd;
int mm;
int yyyy;
};

date d1, d2, d3;

Note In Turbo C++ IDE the memory size used by int data type is 2 Bytes but in Geany IDE it is 4 Bytes. But the data type short uses only 2 Bytes.

c) Dot operator is used to access a single member. Each element of structure can be initialized sepa¬rately ortogether.
Example

#include<iostream>
using namespace std;
int main()
{
struct sample
{
int scode;
char sname[25];
}st1 ={8172,"BVM Kalparambu”};
cout<<“The details of our school is “<<endl;
cout<<“Scool Code\t:”<<st1.scode<<endl;
cout<<“Scool Name\t:”<<st1.sname<<endl;
}

Example:

#include<iostream>
using namespace std;
int main()
{
struct student
{
int reg_no;
charname[40];
char sex;
short ce,pe,te,total;
}st1;
cout<<”Enter Reg No:”;
cin>>stl .reg_no;
cout<<”Enter Name:”;
cin>>stl .name;
cout<<Enter sex:”;
cin>>stl .sex;
cout<<”Enter ce,pe and te scores:”;
cin>>stl .ce>>stl .pe>>stl .te;
st1 .total=stl .ce+stl .pe+stl .te;
cout<<"\ηThe details of Studenti is given below”;
cout<<”Reg\t:”<<stl .reg_noc<endl;
cout<<"Name\t:”<<stl .namec<endl;
cout<<”Sex\t:”<<stl .sex<<endl;
cout<<”CE Score\t:”ccstl .ce<<endl;
cout<<”PE Score\t:”<<stl .pec<endl;
cout<<”TE Score\t:”<<stl .te<<endl;
cout<<”Total Score\t:”<<stl .total<<endl;
}

d) If a structure declaration contains another structure as one of the members, it is called nested structure.
Example:
Plus Two Computer Science Notes Chapter 1 Structures and Pointers 1
Plus Two Computer Science Notes Chapter 1 Structures and Pointers 2

The pointer is a variable which points to memory loca¬tion of some other variable
Syntax:
data_type *variable;
int *ptr1;- Here ptr1 is a pointer variable that stores the address of an integer(Normally hexa decimal number is used to store the address),

float *ptr2; – Here ptr2 is a pointer variable that stores- the address of a float.
struct date *ptr3; – Here ptr3 is a pointer that stores the address of date data type.

*The operators & and*.
Once a variable is declared, there are two types of values.
1. L-value(Location value)- Each and every byte in the memory has a unique address. L-value is the address of a variable. C++ provides an operator called address operator(&), to get the address of a variable.

Eg.

int n,*ptr1;
n=10;

ptr1=&n; Here the address of the variable is stored in the pointer variable ptr1.

2. R-value (Read value)- The value store in the location or the variable. Indirection or deference operator is used to get the content of a variable.

Eg.
int n,*ptr1;
n=10;
ptr1 =&n; Here the address of the variable is stored in the pointer variable ptr1.
cout<<n;
and
cout<<*ptr1;
Both are same and displayed 10.
Consider the following program and out put. The address may vary.
Plus Two Computer Science Notes Chapter 1 Structures and Pointers 3
Plus Two Computer Science Notes Chapter 1 Structures and Pointers 6

  • Memory Map means the process of dividing the memory into four regions for four different purposes
  • Memory allocation at the compile time is static and during the run time is dynamic.
  • The operator used fordynamic memory allocation is NEW and de allocation of such variables is by DELETE Syntax:

pointer_variable =new datatype;
Eg:
Plus Two Computer Science Notes Chapter 1 Structures and Pointers 4

Memory space available for dynamic memory allocation I free store

Memory leak
If the memory allocated using new operator is not de-allocated using delete , that memory is left unused and not released for further allocation. Such memory blocks are called orphaned memory. On each execution the amount of orphaned block is increased. This situation is called memory leak.

The reasons for memory leak are given below.

  • Forgetting to use the operator delete to delete the allocated memory that has been allocated by new operator.
  • delete operator is not in the scope of program due to poor logic.
  • multiple allocation to a pointer variable.

Operations on pointers.

Arithmetic operations on pointers
Only two arithmetic operations are possible on
pointers, integer addition and integer subtraction.

int n,*ptr;
ptr=&η;

Suppose the address of the variable n is 1000. ptr++ or ptr=ptr+1 or ptr +=1 these all are equal.

It does not mean the address +1 (1000+1=1001.) Instead, it means 1000+(size of data type)=1000+4 =1004(ln Geany the integer data type used 4 bytes of memory instead of 2 Bytes).

Similarly the arithmetic operation subtraction, ptr—or ptr= ptr-1 or ptr-=1 these all are equal.
If the address is 1000. ptr—returns 1000-4=996.
ptr+5 returns 1000+(5×4)=1020,
ptr+10 returns 1000+(10×4)=1040.
if the address is 1000.
ptr-10 returns 1000-(10×4)=960.

Example:

#include<iostream>
using namespace std;
int main()
{
int n,*ptr;
ptr=&n;
cout<<“The address of the variable n is “<<ptr<<endl;
cout<<“The next address is “<<++ptr<<endl;
cout<<“The next address is “<<++ptr<<endl;
cout<<“Note that the address is increased not by 1”<<endl;
cout<<“ but by 4(size of int data type is 4)”<<endl;
}
Plus Two Computer Science Notes Chapter 1 Structures and Pointers 8

Note that the address is represented by hexa decimal number.

Relational Operators on pointers.

Only two relational operations are possible on pointers, equality(==) and non equality(!=)

If ptr1 and ptr2 are two pointers, they may or may not contain the same or different address locations. This can be verified using the == and != operators as follows

ptr1 == ptr2 or ptr1 != ptr2.

Pointer and array

An array is a collection of elements with same data type and the elements are stored in contiguous(neighbouring) locations, int mark[10].; It is an array here we can store 10 elements and the OS allocates 10×4=40 Bytes. The important thing you have to remember is that the starting address of the array is the array name itself. That is ‘mark’ is the starting address of the array. mark[0] is the first element it is equivalent to ‘(mark). mark[1] is the second element it is equivalent to *(mark+1) etc.
Plus Two Computer Science Notes Chapter 1 Structures and Pointers 9

Read the following statements and write the difference between them.
a) int*ptr=newint(10);
b) int *ptr=new int[10];
a) Here ptr is a pointer variable that creates a dynamic memory location to store an integer and stores the value 10 in it.
b) Here ptr is a pointer variable that creates contiguous dynamic memory locations to store 10 integer variables.

Pointer and string

A string is a character array. The name of the string is the base address.
charstr[40]; -Here stris a character array we can store up to 39 characters. The last character is a null character(\0).
char *sptr; – Here sptr is a string pointer. eg.
cin>>str; – To read a string in the variable str.lnput a #include<iostream>
string say “COMPUTER”;
cout<<str; It prints “COMPUTER”
int main()
sptr=str; – This means the content of the string str is copied into the pointer sptr. No need to use strcpy() function to copy a string into another.

The following statements are same and display sptr=str;
“COMPUTER”
1. cout<<str; }
2. cout<<sptr;
3. cout<<&str[0];

The following statements are same and display
‘C’.
1. cout<<str;
2. cout<<sptr;
3. cout<c&str[O];

The following statements are same and display
1. cout<cstr[O];
2. cout<<*sptr;

The following statements are same and display
“OMPUTER”
1. cout<<sptr+1;
2. cout<c&str[1];
Example

#indude<iostream>
using namespace std;
int mam()
{
char str[25],*sptr;
cout<<"Enter a string:;
cin>>str;
sptr=str;
cout<<&str(O]«endl;
cout<<sptr«endl; .
cout<<&str(1]c<endl;
cout<<sptr+1 <<endi;
cout<<&str<<endl;
cout<<&sptr<<endl;
}

Advantages of character pointer
No need to specify the size. int mm;
eg. char*sptr=”Computer”;

No need to use the strcpy() function to copy the strings instead assignment operator(=) can be used

eg.

#indude<iostream>
using namespace std;
int main()
{
char str[]="computer”;
char *sptr;
sptr=str;
cout<<strc<<endl;
cout<<sptr<<endl;
}

Optimal use of memory space.
Any character can be accessed by using the pointer arithmetic and makes access faster.
eg.
Plus Two Computer Science Notes Chapter 1 Structures and Pointers 10
date *ptr;
ptr=new date;

This ptr allocates 4+4+4=12 Bytes of memory and the address is stored ¡n the pointer ptr. Instead of dot(.) operator arrow(->) operator is used to reference the elements.
eg:
Plus Two Computer Science Notes Chapter 1 Structures and Pointers 11
Plus Two Computer Science Notes Chapter 1 Structures and Pointers 12

Self Referential Structures-: A structure contains an element that is a pointer to the same structure.
Eg:

struct date
{
int dd,mm,yyyy;
date *Ptr;
};

Plus Two Computer Science Notes