CS 13A

Prof. Sturm                               HW #4 - pointers

 

(This HW is not to be collected.  Do the exercises at home, and we will go over them in class.)

 

What’s the output?

 

1.

int *p,*q,m=5,n=6;

p=&m;

q=&n;

n=*p;

q=p;

m=*q;

Cout<<m<<n<<endl;

 

2.

int a[3] = {1,15,20};                                                               

int *p;

p=a;

cout<<*a<<*a+2<<*(a+2)<<a[a[0]]<<*p+1<<*(p+2)<<endl;

 

3.

int *a;

int n;

n = 10;

a = new int [n];

for(int i=0; i<10; i++){

            a[i] = i%3;

            cout<<a[i]<<endl;

}

 

4.

void func(int *p,int n){

            *p = 10;

            n = 5;

}

void main(){

            int n=1,m=2;

            func(&m,n);

            cout<<m<<n<<endl;

}

 

 

 

            5.

            void func(int n, int *p, int &m){                                out

*p = n + m + m;

n = n+2;

m=m+2;

return;

            }

            void main(){

                        int a=10,b=20,c=30;

                        func(a,&b,c);

                        cout<<a<<b<<c<<endl;

            }