slide1
Download
Skip this Video
Download Presentation
Lecture# 20 Programming Concepts

Loading in 2 Seconds...

play fullscreen
1 / 32

Lecture# 20 Programming Concepts - PowerPoint PPT Presentation


  • 72 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Lecture# 20 Programming Concepts' - aquila


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
slide1

Lecture# 20

Programming Concepts

pointer to pointer
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
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
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 ) ;
slide5
#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;
slide6
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();
  • }
slide8
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
Structure definition
  • struct Student {
  • char name [ 60 ] ;
  • char address [ 100 ] ;
  • double gpa ;
  • } ;

Structure

name

Keyword

slide11
Student s1 , s2 , s3 ;

Structure Name

Variable Name

structure
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
structure1
Structure
  • struct address {
  • char streetAddress [ 100 ] ;
  • char city [ 30 ] ;
  • char country [ 30 ] ;
  • }
structure2
Structure
  • struct Student {
  • char name [ 60 ] ;
  • address add ;
  • double gpa ;
  • } s1 , s2 , s3 ;
structures1
Structures

Variables of type structure

Pointers to Structure

Array of Structure

structures2
Structures
  • Student a,b,c;
  • Student s [ 100 ] ;
  • Student *sPtr ;
example 2
Example 2
  • struct Student {
  • char name [ 64 ] ;
  • char course [ 128 ] ;
  • int age ;
  • int year ;
  • } ;
initializing structures
Initializing Structures

Student s1 = { “Amir”,“cs201”,19, 2002 } ;

Year

Name

Course

age

initializing structures1
Initializing Structures
  • s1.name ;
  • s1.course ;
  • s1.age ;
  • s1.year ;
initializing structures2
Initializing Structures
  • s1.age = 20 ;
  • s1.name = “Abbas Ali ” ; //Wrong
  • strcpy ( s1.name , “Abbas Ali ” ) ;
  • s1.year=2000;
accessing structure members
Accessing structure members
  • cout << s1.name ;
  • cout << s1.course ;
  • cout << s1.age ;
  • cout << s1.year ;
slide22
#include<string>
  • struct Student{
  • char name [ 64 ];
  • char course [ 128 ];
  • int age;
  • int year;
  • };
  • void main(){
  • Student s1={"Amir","Programming",19,2002},s2;
slide23
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();
  • }
structure3
Structure
  • Student stdnt1 , stdnt2 ;
  • stdnt1 + stdnt2 ; //Wrong
structure4
Structure
  • Student s1 , s2 ;
  • s1 = s2 ;
pointers to structure
Pointers to Structure
  • Student *sPtr , s1 ;
  • sPtr = &s1 ;
  • *sPtr.name ; Wrong
  • *sPtr ->name ;
  • Same as
  • s1.name
arrays of structure
Arrays of Structure
  • Student s [ 100 ] ;
  • s [ 0 ].name ;
  • s [ 1 ].name ;
  • s [ 2 ].name ;
  • . . .
  • s [ 99 ].name ;
passing structures to functions
Passing Structures to Functions

Call by value

Call by Reference  X

slide29
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;
  • }
structure5
Structure
  • Simple Variable of type
  • Structure Pointer to Structure
  • Arrays of Structures
  • Function that returns a Structure
  • Pass the Structure to functions
example 3
Example 3
  • struct Student {
  • char firstName [ 30 ] ;
  • char lastName [ 30 ] ;
  • char course [ 15 ] ;
  • char rollNo [ 10 ] ;
  • int age ;
  • float gpa ;
  • } ;
slide32
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 ;
  • }
ad