1 / 20

# Lecture# 19 Programming Concepts

Lecture# 19 Programming Concepts. Initializing pointer. ptr = &amp;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.

Télécharger la présentation

## Lecture# 19 Programming Concepts

E N D

### Presentation Transcript

1. Lecture# 19 Programming Concepts

2. Initializing pointer • ptr = &var ; • ptr = 0 ; • ptr = NULL ; • 0 and NULL points to nothing

3. Pointer Arithmetic • we can do some arithmetic operations on pointers • ptr++; • ptr--; • yptr + 3 ; • ptr+yptr ; • yptr+3 ;

4. 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; • }

5. const int const *myptr=&x; myptr is a constant pointer to an integer

6. const constint *myptr=&x; myptr is a pointer to a constant integer

7. void f(const int *); • int main(){ • int y=10; • f(&y); • getch(); • return 0; • } • void f(const int *xptr){ • *xptr=100; • }

8. 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]

9. Arrays • The name of the array is like a pointerwhich contain the address of the first element.

10. Array and Pointers • int y [ 10 ] ; • int *yptr ; • yptr = y ; • yptr ++ ; • *yptr ++ ; • yptr +3 ; • y++; // Wrong

11. Array and Pointers • yptr = y ; is same asyptr = &y [ 0 ] ;……..yptr = &y [ 2 ] ;

12. 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

13. The new Operator • int main(){ • float*p; • p=new float;//create unnamed memory • *p=3.14159; • cout<<p<<" "<<*p; • float a=3.14159; • getch(); • }

14. 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

15. Pointer to pointer(**) • int main(){ • int n=44; • int* pn=&n; • int** ppn=&pn; • cout<<"*pn="<<*pn<<" ppn="<<**ppn; • getch(); • } 44 pn ppn

16. 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 ) ;

17. 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 ) ;

18. #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;

19. 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(); • }

More Related