Sie sind auf Seite 1von 3

TUTORIAL 7 EC101A: Computer Systems and Programming Autumn Semester: August December 2011 Dated: 22-09-2011

1.Write a program that declares three variables: first_value and second_value both of type int and mypointer of type int *. Initialize the variables of type int and display their values. Then modify their values through mypointer and display the modified values. Understand the use of address operator (&) and dereferencing operator (*). 2.Write a function that uses pointers to search for the address of a given integer in a given array. If the given integer is found, the function returns its address; otherwise it returns NULL. 3. Execute the following program #include <iostream> #include <string> using namespace std; int main(){ char name='a'; int anint=10; double afloat=1.2; cout << name << " , size of var is : " << sizeof(name) << " , size of ptr is : " << sizeof(&name)<< endl; cout << anint << " , size of var is : " << sizeof(anint) << " , size of ptr is : " << sizeof(&anint)<< endl; cout << afloat << " , size of var is : " << sizeof(afloat) << " , size of ptr is : " << sizeof(&afloat)<< endl; return 0; } Are size of var and size of ptr same for a datatype? Are size of var same for all the datatypes? Are size of ptr same for all the datatypes? In all the above cases, explain and justify your answer. (Hint: What is the difference between sizeof(name) and sizeof(&name)? 4.Explain what is a function pointer? How do you declare a function pointer for a. int calcarea(int x, int y); b. char printmyname(string s); c. int randomfunc(float x, double y, int z); d. int mytime(int a, int b); e. float nicefunc(int i, int j); Can the function pointer for part (a) above be used for part (d)? Can the function pointer for part (a) above be used for part (e)?

Why or why not? Explain your answer in both cases. 5..Run the program given below and understand the display. int main( ){ int *p; p = new int; *p = 66; cout<<"Before call to function *p = "<<*p<<endl; showValue (p); cout<<"After call to function *p = "<<*p<<endl; cin.ignore( ); cin.get(); return 0; } void showValue ( int *temp) { cout<<"Inside "<<*temp<<endl; *temp = 77; cout<<"Inside function call before exit *temp = "<<*temp<<endl; } 6..Assume that a two-dimensional array A of type int and of size 3 x 3 has been initialized. Using pointers, display the addresses of its elements and the contents of these addresses row-wise. 7.. Write a program that takes the size n of an array from the user.It then dynamically creates an array of size n and type double.It should prompt the user to input n values (of type double) which are then stored in the array.Also write a recursive function sum whose prototype is double sum( *a,int size) and it recursively finds the sum of the entire array.The function should finally return the sum which should be displayed by the main in your program. 8. Write definitions of functions: void inputNumber ( int * ) and void doubleValue (int* ). inputNumber function asks user for a integer number and stores it in the space pointed by input parameter. The function doubleValue doubles the value provided by the parameter. Write main to test these two functions. 9. Write a function that uses pointers to search for the address of a given integer in a given array. If the given integer is found, the function returns its address; otherwise it returns NULL. function call in the beginning *temp =

10. Explain what is a function pointer? How do you declare a function pointer for f. int calcarea(int x, int y); g. char printmyname(string s); h. int randomfunc(float x, double y, int z); 11. The prototype of a function which has a pointer to function as pf and sums the square and cube of even integers up to n. is given below int sum ( int(*) (int), int) Using this proto type make a complete program. Hint:-1. use square and cube function prototypes as int square (int) , int cube(int) Hint-2. If n=4 , then sum of square is=4+16=20 ,and sum of cube is 8+64=100 12. Having known the desired size of an array, allocate memory for it with the new operator and save the address of that dynamic memory in a pointer. Pointers may be subscripted just as arrays are. Using integer input for size of one dimensional array, intialize the elements of the array to zero. Display the contents of array. After display, free the dynamically allocated memory.