Chapter 5

 

 

 


Text Box:  
Pointer Operators

#include <iostream.h>
main()
{
	int ferrari = 18;
	int *ferrariPtr;

	ferrariPtr=&ferrari; /* The pointer ferrari is assigned the value of 
the address of Ferrari */
	// ferrariPtr is said to “point to” ferrari 

	cout<<&ferrari<<endl;  // the address
	cout<<ferrariPtr<<endl; // the pointer

	cout<<ferrari<<endl;  // the integer
	cout<<*ferrariPtr<<endl;  /* Dereferencing operator (*) returns the 
object the pointer is pointing to */

	return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Text Box:  
Call-By-Reference

void modify (float *);

main()
{
	float lamborghini=12.16;
	
	cout<<"At first lamborghini is "<<lamborghini<<"."<<endl<<endl; 
	
	modify (&lamborghini); /* The address is passed to the function so 
   that lamborghini can be modified and still be a local variable */
	
	cout<<"After being modified in the MAGIC CALL-BY-REFERENCE FUNCTION" 
    <<",\n lamborghini is "<<lamborghini<<endl;

	return 0;
}

void modify (float *lPtr)
{
	*lPtr=6.08;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Text Box:  
Pointer/Subscript Notation

#include <iostream.h>
main()
{
	int aston[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
	int *martin = aston; // martin points to aston

	for (int i=0; i<=9; i++)    
		cout<<martin[i]<<" ";  /* martin[i] refers to the array 
     element aston[i] */

	return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Text Box:  
String Handling Library Functions (strcpy, strncat, and strcmp)

#include <iostream.h>
#include <string.h> /* Header function for functions from the string 
  handling library */

main()
{
	char car1[]="porsche";
	char car2[]=" carrera";
	char empty[10];

	cout<<"car1:"<<car1<<endl;
	cout<<"car2:"<<car2<<endl<<endl
    <<"(the space before the word 'carrera' will make a difference" 
    <<" when  we compare the two arrays)"<<endl<<endl;
	
	
	strcpy (empty, car1); /* Copies string car1 into the empty array and 
couts the empty array */
	
cout<<"String car1 is copied into the empty array. "
    <<"Now the empty array is: "<<empty<<endl<<endl;
	

	cout<<"Now let's compare string car1 to string car2."<<endl;
	cout<<"A "<<strcmp (car1, car2)<<" is returned, so ";
	/* strcmp (car1, car2) compares string car1 to string car2 and 
returns an integer */
	
if (strcmp (car1, car2)>0)
		cout<<"array car2 is longer than array car1."<<endl<<endl;
	
	else if (strcmp (car1, car2)==0)
		cout<<"array car2 is the same as array car1."<<endl<<endl;
	
	else
		cout<<"array car2 is shorter than array car1."<<endl<<endl;


	cout<<"Lastly, we can append the two strings and make a supercar!"
    <<endl;
	cout<<strcat (car1, car2)<<endl<<endl; /* Appends string car2 to 
   string car1 */

	return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Text Box:  
strtok

#include <iostream.h>
#include <string.h>

main()
{
	char b[]="Saleen S7 Twin Turbo";
	char *bPtr;

	bPtr=strtok (b, " ");

	cout<<bPtr<<endl<<endl;	// Prints the first element of string b

	while (bPtr != NULL)
	{
		cout<<bPtr<<'\n';
		bPtr = strtok(NULL, " ");
	}

	return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Text Box:  
strlen

#include <iostream.h>
#include <string.h>

main()
{
	char m[]="Maserati";

	cout<<"The string '"<<m<<"' has "
		<<strlen (m)<<" characters."<<endl<<endl;

	return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Text Box:  
cin.getline

#include <iostream.h>

main()
{
	char m[30];
	cin.getline(m, 30, '\n');

	cout<<m<<endl;

	return 0;
}