1 / 32

Lecture# 20 Programming Concepts

Lecture# 20 Programming Concepts. 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 ) ;

aquila
Télécharger la présentation

Lecture# 20 Programming Concepts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture# 20 Programming Concepts

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

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

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

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

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

  7. void main(){ • char *p; • char a[100],b[15]; • cout<<"Plz Enter a string:"; • gets(a); • cout<<"What u want find"; • gets(b); • p=strtok(a," "); • while(p!=NULL){ • if(strcmp(b,p)==0) • cout<<"\n The string\" "<<p<< "\"is found:"; • p=strtok(NULL," "); • } • getch(); • }

  8. Structures

  9. Structure definition • struct Student { • char name [ 60 ] ; • char address [ 100 ] ; • double gpa ; • } ; Structure name Keyword

  10. Student s1 , s2 , s3 ; Structure Name Variable Name

  11. Structure • Structure can have pointer data type • Structure can have other structure as member data type • Structure can not have same structure as data type

  12. Structure • struct address { • char streetAddress [ 100 ] ; • char city [ 30 ] ; • char country [ 30 ] ; • }

  13. Structure • struct Student { • char name [ 60 ] ; • address add ; • double gpa ; • } s1 , s2 , s3 ;

  14. Structures Variables of type structure Pointers to Structure Array of Structure

  15. Structures • Student a,b,c; • Student s [ 100 ] ; • Student *sPtr ;

  16. Example 2 • struct Student { • char name [ 64 ] ; • char course [ 128 ] ; • int age ; • int year ; • } ;

  17. Initializing Structures Student s1 = { “Amir”,“cs201”,19, 2002 } ; Year Name Course age

  18. Initializing Structures • s1.name ; • s1.course ; • s1.age ; • s1.year ;

  19. Initializing Structures • s1.age = 20 ; • s1.name = “Abbas Ali ” ; //Wrong • strcpy ( s1.name , “Abbas Ali ” ) ; • s1.year=2000;

  20. Accessing structure members • cout << s1.name ; • cout << s1.course ; • cout << s1.age ; • cout << s1.year ;

  21. #include<string> • struct Student{ • char name [ 64 ]; • char course [ 128 ]; • int age; • int year; • }; • void main(){ • Student s1={"Amir","Programming",19,2002},s2;

  22. strcpy( s2.name,"Abbas Ali"); • s2.age=23; • cin>>s2.year; • cout<<s1.name<<" "<<s2.name<<endl; • cout<<s1.course<<" "<<s2.course<<endl; • cout<<s1.age<<" "<<s2.age<<endl; • cout<<s1.year<<" "<<s2.year<<endl; • getch(); • }

  23. Structure • Student stdnt1 , stdnt2 ; • stdnt1 + stdnt2 ; //Wrong

  24. Structure • Student s1 , s2 ; • s1 = s2 ;

  25. Pointers to Structure • Student *sPtr , s1 ; • sPtr = &s1 ; • *sPtr.name ; Wrong • *sPtr ->name ; • Same as • s1.name

  26. Arrays of Structure • Student s [ 100 ] ; • s [ 0 ].name ; • s [ 1 ].name ; • s [ 2 ].name ; • . . . • s [ 99 ].name ;

  27. Passing Structures to Functions Call by value Call by Reference  X

  28. struct Student{ • char name[20]; • int rollno; • }; • void init(Student&); • void main(){ • Student s1; • init(s1); • cout<<s1.name<<" "<<s1.rollno; • getch(); • } • void init(Student &a){ • cout<<"Plz enter the name \n"; • gets(a.name); • cout<<"Plz enter the Roll No \n"; • cin>>a.rollno; • }

  29. Structure • Simple Variable of type • Structure Pointer to Structure • Arrays of Structures • Function that returns a Structure • Pass the Structure to functions

  30. Example 3 • struct Student { • char firstName [ 30 ] ; • char lastName [ 30 ] ; • char course [ 15 ] ; • char rollNo [ 10 ] ; • int age ; • float gpa ; • } ;

  31. Student s [ 10 ] ; • for ( int i = 0 ; i < 10 ; i ++ ) { • cout << “Please enter the student's last name : " ; • cin >> s [ i ].lastName ; • cout << “Please enter the student's first name : " ; • cin >> s [ i ].firstName ; • cout << " Please enter the student's course : " ; • cin >> s [ i ].course ; • cout << " Please enter the student's Roll No. : " ; • cin >> s [ i ].rollNo ; • cout << " Please enter the student's grade : " ; • cin >> s [ i ].grade ; • cout << " Please enter the student's age : " ; • cin >> s [ i ].age ; • cout << " Please enter the student's GPA : " ; • cin >> s [ i ].gpa ; • }

More Related