Exercises
Exercises
1)What does the below print ? Explain your answer.You can use the asciii chart at:
http://www.asciitable.com/
File: exer1.cpp
#include <iostream> using namespace std; int main() { int var1 = 32, *ptr = &var1 ; char ch = 'A', &cho = ch ; cho += var1 ; *ptr += ch; cout << var1 << ", " << ch << endl; return 0; }
2) What is the output of the following program ? Does it compile correctly ?
File: exer2.cpp
#include <iostream> using namespace std; int main() { const int i1 = 20; const int* const ptr = &i1 ; (*ptr)++; int j1 = 15; ptr = &j1 ; cout << i1 ; return 0; }3) What is the output of the following program ?
File: exer3.cpp
#include <iostream> using namespace std; int main() { int num[5]; int* ptr ; ptr = num; *ptr = 10; ptr++; *ptr = 20; ptr = &num[2]; *ptr = 30; ptr = num + 3; *ptr = 40; ptr = num; *(ptr + 4) = 50; for (int i1 = 0; i1 < 5; i1++) cout << num[i1] << ", "; return 0; }
4) What is the output of the following program ?
File: exer4.cpp
#include <iostream> using namespace std; int main() { int arr[] = { 4, 5, 6, 7 } ; int* ptr = (arr + 1) ; cout << *arr + 10; return 0; }5) What is the output of the following program ?
File: exer5.cpp
#include <iostream> using namespace std; int main() { int a1 = 10, *pa, &ra ; pa = &a1 ; ra = a1 ; cout << "a1=" << ra ; return 0; }6) What is the difference between
int (*ptr)[5] ; and int *ptr[5] ; Show how the above are used by creating small sample programs .
7) What is the output of the following program ?
File: exer7.cpp
#include <stdio.h> int main() { int *ptr ; int x1 ; ptr = &x1 ; *ptr = 0 ; printf(" x1 = %d\n", x1) ; printf(" *ptr = %d\n", *ptr) ; *ptr += 5; printf(" x1 = %d\n", x1) ; printf(" *ptr = %d\n", *ptr) ; (*ptr)++ ; printf(" x1 = %d\n", x1) ; printf(" *ptr = %d\n", *ptr) ; return 0; }
8) What is the output of the following program ?
File: exer8.cpp
#include <stdio.h> int main() { float arr[5] = {12.5, 10.0, 13.5, 90.5, 0.5}; float *ptr1 = &arr[0]; float *ptr2 = ptr1 + 3; printf("%f ", *ptr2); printf("%d", ptr2 - ptr1); return 0; }
9) What is the output of the following program ?
File: exer8.cpp
#include<stdio.h> int main() { int arr[] = {10, 20, 30, 40, 50, 60}; int *ptr1 = arr; int *ptr2 = arr + 5; printf("Number of elements between two pointer are: %d\n", (ptr2 - ptr1)); printf("Number of bytes between two pointers are: %d\n", (char*)ptr2 - (char*) ptr1); return 0; }
10) What is the output of the following program ?
File: exer10.cpp
#include <iostream> using namespace std ; int main() { int x1[5] = { 1, 2, 3, 4, 5 }; // ptr points to array x1 int* ptr = x1; int i1; // exchange values usi1ng pointer for (i1 = 0; i1 < 2; i1++) { int temp = *(ptr + i1) ; *(ptr + i1) = *(ptr + 4 - i1) ; *(ptr + 4 - i1) = temp ; } // output the array x1 for (i1 = 0; i1 < 5; i1++) cout << x1[i1] << " "; return 0; }
11) What is the output of the following program ?
File: exer11.cpp
#include <iostream> using namespace std ; //----------------------------------------------------------- int main() { char c = 'T', d = 'S' ; char *p1 = &c ; char *p2 = &d ; char *p3 ; p3 = &d; cout << "*p3 = " << *p3 << endl ; p3 = p1; cout << "*p3 = " << *p3 << endl ; *p1 = *p2; cout << "*p1 = " << *p1 << endl ; return( 0 ) ; } //-----------------------------------------------------------
Solutions
6)File: soln6.cpp
#include <iostream> using namespace std; int main() { //Almost same as int* ptr but can only //point to a block of 5 integers int (*ptr1)[5] ; //An array of 5 pointers int *ptr2[5] ; int x1 = 100 ; int arr1[5]={ 1, 2, 3, 4, 5 } ; ptr1 = &arr1 ; ptr2[0] = &x1 ; return 0; }