Lecture 19 Ex

(1)
(a)
What is the meaing of * in the following code?
int* pt;
(b)
What is the meaing of * in the following code?
int* pt=new int;
*pt = 5;
(c)
What is the meaning of & in the following code?
int function(int& x, int y);
(d)
What is the meaning of & in the following code?
int x;
int *pt = &x;
(2)
Which of the following variables are pointers?
#include<iostream>
using namespace std;

typedef int* IntPtr;

int main() {
    int *a, b, *c;
    int* d, e, f;
    IntPtr i, j, k;

    return 0;   
}
(3)
(a)What is the output of the following program?
#include <iostream>
using namespace std;

int main() {
    char *pt, ch='a'; 
    pt = &ch;
    cout << ch << endl;
    cout << *pt << endl;
    ch = 'c';
    cout << ch << endl;
    cout << *pt << endl;
    *pt = 'e';
    cout << ch << endl;
    cout << *pt << endl;
    return 0;
}
(b)What is the output of the following program?
#include <iostream>
using namespace std;

int main() {
    char *pt, ch1='a', ch2='b'; 
    pt=&ch1;
    cout << ch1 << "," << ch2 << "," << *pt << endl;
    
    ch1='A';
    ch2='B';
    cout << ch1 << "," << ch2 << "," << *pt << endl;
    
    *pt='P';
    cout << ch1 << "," << ch2 << "," << *pt << endl;

    pt=&ch2;
    cout << ch1 << "," << ch2 << "," << *pt << endl;
    
    ch1='X';
    ch2='Y';
    *pt='Z';
    cout << ch1 << "," << ch2 << "," << *pt << endl;

    pt=&ch1;
    cout << ch1 << "," << ch2 << "," << *pt << endl;

    ch1=ch2;
    *pt=ch2;
    ch2='#';
    ch1='A';
    cout << ch1 << "," << ch2 << "," << *pt << endl;

    pt=new char;
    ch1='1';
    ch2='2';
    *pt='3';
    cout << ch1 << "," << ch2 << "," << *pt << endl;

    return 0;
}
(c) What is the output of the following program?
#include <iostream>
using namespace std;

int main() {
    int *pt1, *pt2, x=5, y=7;
    pt1=&x;
    pt2=&y;
    cout << x << "," << *pt1 << "," << y << "," << *pt2 << endl;
    
    (*pt1)+=5;
    y--;
    cout << x << "," << *pt1 << "," << y << "," << *pt2 << endl;
    
    *pt1=9;
    *pt2=10;
    cout << x << "," << *pt1 << "," << y << "," << *pt2 << endl;

    pt1=&y;
    y++;
    cout << x << "," << *pt1 << "," << y << "," << *pt2 << endl;

    pt2=pt1;
    x=100;
    cout << x << "," << *pt1 << "," << y << "," << *pt2 << endl;

    pt1=new int;
    *pt1=37;
    cout << x << "," << *pt1 << "," << y << "," << *pt2 << endl;

    pt2=new int;
    pt1=&x;
    *pt2=22;
    *pt1=87;
    y=19;
    cout << x << "," << *pt1 << "," << y << "," << *pt2 << endl;

    y=x;
    x=-3;
    cout << x << "," << *pt1 << "," << y << "," << *pt2 << endl;

    *pt2=*pt1;
    y=1;
    cout << x << "," << *pt1 << "," << y << "," << *pt2 << endl;
  
    return 0;
}

(4)
(a)Find out the compiling error and correct it.
#include <iostream>
using namespace std;

int main() {
   int* pt1, pt2;
   pt2=new int;

   return 0;
}
(b)Find out the compiling error and correct it.
#include <iostream>
using namespace std;

int main() {
   int* pt, x;
   pt = x;

   return 0;
}
(c)Are there any compiling errors in the following program? any run time error? correct the errors.
#include <iostream>
using namespace std;

int main() {
   int* pt, x=6;
   cout << x;
   cout << *pt;

   return 0;
}