C++ Programming - Functions - Discussion
Discussion Forum : Functions - Programs (Q.No. 30)
30.
Which of the following statement is correct about the program given below?
#include<iostream.h>
#include<string.h>
#include<malloc.h>
class BixString
{
char txtName[20];
public:
BixString(char *txtTemp = NULL)
{
if(txtTemp != NULL)
strcpy(txtName, txtTemp);
}
void Display(void)
{
cout<<txtName;
}
};
int main()
{
char *txtName = (char*)malloc(10);
strcpy(txtName, "IndiaBIX");
*txtName = 48;
BixString objTemp(txtName);
cout<< sizeof(txtName);
return 0;
}
Discussion:
18 comments Page 1 of 2.
Moron said:
6 years ago
@All.
The answer depends on the platform that we are running this program and is either 4 or 8 (the size of the pointer on x86 and x64 respectively). And there is no code to print the content of the string.
The answer depends on the platform that we are running this program and is either 4 or 8 (the size of the pointer on x86 and x64 respectively). And there is no code to print the content of the string.
(2)
Vaishali said:
5 years ago
txtName = "IndiaBIX" which is of size 8.
then *txtName = 48 which modifies txtName as 0ndiaBIX.
which is again of size 8 so answer is 8.
then *txtName = 48 which modifies txtName as 0ndiaBIX.
which is again of size 8 so answer is 8.
(1)
Bhavesh said:
8 years ago
Here strlen return size of string as 8. But the string is followed by null character '\0' so size will be increased by 1 more bit, Finally, size becomes 8+1 = 9.
That's why output is IndiaBix 9.
That's why output is IndiaBix 9.
(1)
Ashish kshatriya said:
9 years ago
The correct answer will be 4 which is size of int, so it is option C.
Arpan said:
5 years ago
@All.
According to me, the Correct code to get IndiaBIX 8.
#include<iostream>
using namespace std;
#include<string.h>
#include<malloc.h>
class BixString
{
char txtName[20];
public:
BixString(char *txtTemp = NULL)
{
if(txtTemp != NULL)
strcpy(txtName, txtTemp);
}
void Display(void)
{
cout<<txtName;
}
};
int main()
{
char *txtName = (char*)malloc(10);
strcpy(txtName, "IndiaBIX");
//*txtName = 48;
BixString objTemp(txtName);
objTemp.Display();
cout<<" "<<sizeof(txtName);
return 0;
}
According to me, the Correct code to get IndiaBIX 8.
#include<iostream>
using namespace std;
#include<string.h>
#include<malloc.h>
class BixString
{
char txtName[20];
public:
BixString(char *txtTemp = NULL)
{
if(txtTemp != NULL)
strcpy(txtName, txtTemp);
}
void Display(void)
{
cout<<txtName;
}
};
int main()
{
char *txtName = (char*)malloc(10);
strcpy(txtName, "IndiaBIX");
//*txtName = 48;
BixString objTemp(txtName);
objTemp.Display();
cout<<" "<<sizeof(txtName);
return 0;
}
Gunasekaran said:
6 years ago
The sizeof operator gives the total size occupied by the address. In this case, its 8. So it should be 8. The String would not be displayed as the Display function was never called.
Vamshi said:
7 years ago
The main thing *txtName = 48; was replace the first char with 48 which means zero('0') now malloc allocate "0ndiaBIX" now size is 9.
Neha said:
7 years ago
According to me, The output is 4.
Sadik Khan said:
8 years ago
The correct answer is 4 size of pointer.
Prabha said:
1 decade ago
The answer is coming out to be 4 which is the size of integer. So correct answer should be option C.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers