C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » C++ programming language

Find output of C++ programs (pointers)

Here, you will find output of pointer based programs in C++, you have to find their outputs. Outputs and their explanations are exists here.
Submitted by Abhishek Jain, on July 01, 2017

1. Find the output of following C++ program.

#include<iostream>
#include<stdlib.h>
using namespace std;

int main()
{ 
	float x=5.999;
	float *y,*z;
	y=&x;
	z=y;
	cout<<x<<","<<*(&x)<<","<<*y<<","<<*z<<"\n";
	return 0;
}

Output

5.999,5.999,5.999,5.999

Explanation

The reason for this is x gives the value stored in the variable x. *(&x) gives the data value stored in the address &x i.e., the data value of x. Since y points to x (..y=&x), *y gives the value of x. And because z has the same address as that of y, *z also gives the value of x i.e., 5.999.

2. Find the output of following C++ program.

#include<iostream>
using namespace std;

int main()
{  
	int track[]={10,20,30,40},*striker;

	striker=track;
	track[1]+=30;
	cout<<"Striker>"<<*striker<<endl;
	*striker-=10;
	striker++;
	cout<<"Next@"<<*striker<<endl;
	striker+=2;
	cout<<"Last@"<<*striker<<endl;
	cout<<"Reset To"<<track[0]<<endl;

	return 0;
}

Output

Striker>10
Next@50
Last@40
Reset To0

Explanation

The array track contains 4 elements {10,20,30,40} and the pointer striker holds the base address of the array track i.e, address of track[0].
1)*striker holds the data value of track[0] i.e, 10. Decrement in *striker by 10 makes the track[0]=0.
2)Incrementing pointer striker gives the next location of track i.e.,1. Now *striker gives the data value of track[1].
3) Again by incrementing by 2 , striker reaches to the 4 address of the array track i.e, track[4].
4)At last print the value at track[0] ,which is 0 (see step 1).

3. Find the output of following C++ 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;
}

Output

129,a

Explanation

ptr is a pointer which holds the address of a while *ptr returns the data value of a. Cho is a reference variable which hold the reference of ch. Now, incrementing the value of cho by 32 (ASCIIvalue), reflect to cho and ch makes it equal to "a" (alphabet). In last step, data value of *ptr incremented by ch i. e., "a" gives a=129.

4. Find the output of following C++ program.

#include<iostream>
using namespace std;

int main()
{ 
	const int i=20;
	const int *const ptr=&i;
	(*ptr)++;
	int j=15;
	ptr=&j;
	return 0;
}

Output

Compilation  Error

Explanation

ptr is a constant pointer to constant integer ,which means neither the pointer nor its contents be modified , thus lines 6 and 8 are invalid as they are trying to modify the contents and pointer respectively.

5. Find the output of following C++ program.

#include<iostream>
#include<cstring>
using namespace std;

int main()
{
	char *s="GOODLUCK";
	for(int i=strlen(s)-1;i>=0;i--)
	{
		for(int j=0;j<=i;j++)
		cout<<s[j];
		cout<<endl;
	}
	return 0;
}

Output

GOODLUCK
GOODLUC
GOODLU
GOODL
GOOD
GOO
GO
G

Explanation

Pointer s points to the first element of the string "GOODLUCK". By using string length function (strlen()), find the length of the string and then through nested loops above pattern is formed.









COMMENTS