200 likes | 330 Vues
Lecture# 19 Programming Concepts. Initializing pointer. ptr = &var ; ptr = 0 ; ptr = NULL ; 0 and NULL points to nothing. Pointer Arithmetic. we can do some arithmetic operations on pointers ptr++; ptr--; yptr + 3 ; ptr+yptr ; yptr+3 ; . Pointer Arithmetic.
E N D
Lecture# 19 Programming Concepts
Initializing pointer • ptr = &var ; • ptr = 0 ; • ptr = NULL ; • 0 and NULL points to nothing
Pointer Arithmetic • we can do some arithmetic operations on pointers • ptr++; • ptr--; • yptr + 3 ; • ptr+yptr ; • yptr+3 ;
Pointer Arithmetic • A pointer can be assigned to the other pointer if both are of same type • int main(){ • int a=10,*b,*c; • b=&a; • c=b; • cout<<*b<<*c; • }
const int const *myptr=&x; myptr is a constant pointer to an integer
const constint *myptr=&x; myptr is a pointer to a constant integer
void f(const int *); • int main(){ • int y=10; • f(&y); • getch(); • return 0; • } • void f(const int *xptr){ • *xptr=100; • }
Array • int a[10]; a a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
Arrays • The name of the array is like a pointerwhich contain the address of the first element.
Array and Pointers • int y [ 10 ] ; • int *yptr ; • yptr = y ; • yptr ++ ; • *yptr ++ ; • yptr +3 ; • y++; // Wrong
Array and Pointers • yptr = y ; is same asyptr = &y [ 0 ] ;……..yptr = &y [ 2 ] ;
The new Operator • float* p; • only allocate memory for a pointer • *p=3.1459 • One way to avoid this is assign memory using the key word new
The new Operator • int main(){ • float*p; • p=new float;//create unnamed memory • *p=3.14159; • cout<<p<<" "<<*p; • float a=3.14159; • getch(); • }
The delete Operator • The delete operator reverse the operation of new operator • It de-allocate the memory reserved by the new operator • float*p=new float; • *p=3.1459; • delete p; • *p=3.44; //Wrong
Pointer to pointer(**) • int main(){ • int n=44; • int* pn=&n; • int** ppn=&pn; • cout<<"*pn="<<*pn<<" ppn="<<**ppn; • getch(); • } 44 pn ppn
String Manipulation Functions • char * strcpy (char *s1 , const char *s2 ) ; Copies the string s2 into the character array s1.The value of s1 returned. • char * strncpy ( char *s1 , char *s2 , int n ) ; copies atmost n characters • char * strcat (char *s1 , char *s2 ) ; Appends the string s2 to the string s1.the first character of s2 overwrite the null character of s1.the value of s1 is returned. • char * strncat ( char *s1 , char *s2 , int n ) ;
Comparison Functions • int strcmp (const char *s1 , const char *s2 ) Compares the string s1 with s2.The function return a value zero,less than zero or greater than zero. • int strncmp ( const char *s1 , const char *s2 , int n ) ; • int strlen ( const char *s ) ;
#include<iostream> • #include<string> • void main(){ • char x[100]={"Happy Birthday"}; • char y[100],z[100]; • strncpy(y,x,5); • strcpy(z,y); • strcat(x," to you"); • cout<<"\nThe x string:"<<x;
if(strcmp(x,y)==0) • cout<<"\n string x and y are equal"; • else • cout<<"\n string x and y are not equal"; • if(strcmp(z,y)==0) • cout<<"\n string z and y are equal"; • else • cout<<"\n string z and y are not equal"; • getch(); • }