320 likes | 426 Views
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 ) ;
E N D
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 ) ; 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(); • }
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(); • }
Structure definition • struct Student { • char name [ 60 ] ; • char address [ 100 ] ; • double gpa ; • } ; Structure name Keyword
Student s1 , s2 , s3 ; Structure Name Variable Name
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
Structure • struct address { • char streetAddress [ 100 ] ; • char city [ 30 ] ; • char country [ 30 ] ; • }
Structure • struct Student { • char name [ 60 ] ; • address add ; • double gpa ; • } s1 , s2 , s3 ;
Structures Variables of type structure Pointers to Structure Array of Structure
Structures • Student a,b,c; • Student s [ 100 ] ; • Student *sPtr ;
Example 2 • struct Student { • char name [ 64 ] ; • char course [ 128 ] ; • int age ; • int year ; • } ;
Initializing Structures Student s1 = { “Amir”,“cs201”,19, 2002 } ; Year Name Course age
Initializing Structures • s1.name ; • s1.course ; • s1.age ; • s1.year ;
Initializing Structures • s1.age = 20 ; • s1.name = “Abbas Ali ” ; //Wrong • strcpy ( s1.name , “Abbas Ali ” ) ; • s1.year=2000;
Accessing structure members • cout << s1.name ; • cout << s1.course ; • cout << s1.age ; • cout << s1.year ;
#include<string> • struct Student{ • char name [ 64 ]; • char course [ 128 ]; • int age; • int year; • }; • void main(){ • Student s1={"Amir","Programming",19,2002},s2;
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(); • }
Structure • Student stdnt1 , stdnt2 ; • stdnt1 + stdnt2 ; //Wrong
Structure • Student s1 , s2 ; • s1 = s2 ;
Pointers to Structure • Student *sPtr , s1 ; • sPtr = &s1 ; • *sPtr.name ; Wrong • *sPtr ->name ; • Same as • s1.name
Arrays of Structure • Student s [ 100 ] ; • s [ 0 ].name ; • s [ 1 ].name ; • s [ 2 ].name ; • . . . • s [ 99 ].name ;
Passing Structures to Functions Call by value Call by Reference X
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; • }
Structure • Simple Variable of type • Structure Pointer to Structure • Arrays of Structures • Function that returns a Structure • Pass the Structure to functions
Example 3 • struct Student { • char firstName [ 30 ] ; • char lastName [ 30 ] ; • char course [ 15 ] ; • char rollNo [ 10 ] ; • int age ; • float gpa ; • } ;
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 ; • }