Passing arrays and pointers to functions.
1)
What will be the output of the following program ?
#include <iostream>
using namespace std;
int main()
{
int a = 32, *ptr = &a;
char ch = 'A', &cho = ch;
cho += a;
*ptr += ch;
cout << a << ", " << ch << endl;
return 0;
}
2) What is the output of the following program ? Does it compile correctly ?
#include <iostream>
using namespace std;
int main()
{
const int i = 20;
const int* const ptr = &i;
(*ptr)++;
int j = 15;
ptr = &j;
cout << i;
return 0;
}
3) What is the output of the following program ?
#include <iostream>
using namespace std;
int main()
{
int num[5];
int* p;
p = num;
*p = 10;
p++;
*p = 20;
p = &num[2];
*p = 30;
p = num + 3;
*p = 40;
p = num;
*(p + 4) = 50;
for (int i = 0; i < 5; i++)
cout << num[i] << ", ";
return 0;
}
4) What is the output of the following code ?
#include <iostream>
using namespace std;
int main()
{
int arr[] = { 4, 5, 6, 7 };
int* p = (arr + 1);
cout << *arr + 10;
return 0;
}
5) What is the output of the following code ? Does it compile correctly ?
#include <iostream>
using namespace std;
int main()
{
int a = 10, *pa, &ra;
pa = &a;
ra = a;
cout << "a=" << 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)
#include <stdio.h>
int main()
{
int *ptr;
int x;
ptr = &x;
*ptr = 0;
printf(" x = %dn", x);
printf(" *ptr = %dn", *ptr);
*ptr += 5;
printf(" x = %dn", x);
printf(" *ptr = %dn", *ptr);
(*ptr)++;
printf(" x = %dn", x);
printf(" *ptr = %dn", *ptr);
return 0;
}
8) What is the output of the following program ?
#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)
#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.",
(ptr2 - ptr1));
printf("Number of bytes between two pointers are: %d",
(char*)ptr2 - (char*) ptr1);
return 0;
}
10)
What would be printed from the following C++ program?
#include <iostream>
using namespace std;
int main()
{
int x[5] = { 1, 2, 3, 4, 5 };
// p points to array x
int* p = x;
int i;
// exchange values using pointer
for (i = 0; i < 2; i++) {
int temp = *(p + i);
*(p + i) = *(p + 4 - i);
*(p + 4 - i) = temp;
}
// output the array x
for (i = 0; i < 5; i++)
cout << x[i] << " ";
return 0;
}
11) What does the following print ?
#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; // (1)
p3 = p1;
cout << "*p3 = " << *p3 << endl; // (3)
*p1 = *p2;
cout << "*p1 = " << *p1 << endl ; // (4)
return( 0 ) ;
}
//-----------------------------------------------------------
12)
What does the following print ?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int x1 = 100 ;
int* ptr1 = &x1 ;
printf( "%p %p\n" , &*ptr1, *&ptr1 ) ;
return ( 1 ) ;
}