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; 
}
Above program will display IndiaBIX 8.
Above program will display IndiaBIX 9.
Above program will display size of integer.
Above program will display IndiaBIX and size of integer.
Above program will display 1.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
18 comments Page 1 of 2.

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;
}

Gaurav said:   1 decade ago
There is the "char *txtName = (char*)malloc(10);" statement. In which malloc operator has reserved the space of the size of 10 for *txtName. It does not matter which size of string is going to store over there. And strlen function count from 0 number. So the answer is 9.

Aakash said:   1 decade ago
@Prabha. The answer is 4 but it's actually displaying the size of pointer which is constant(4 on 64 bit machine & 2 on 32 bit machine) for all data types. sizeof(*ptr) displays the size of corresponding data type.

Manoj said:   1 decade ago
@Janson and @Ankuran is right it gives the output 8 as Display function is not called so it's only gives output 8 nothing else.

If we call the display function then it will print output 8 as size and 0ndiaBIX.

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.
(2)

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.
(1)

Ankuran said:   1 decade 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.

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.

Jaison said:   1 decade ago
A is correct length of string is 8. Check your online compiler for c++.

After assigning of *txtName = 48;.

The string become "0ndiaBIX" and sizeof string is 8.

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.
(1)


Post your comments here:

Your comments will be displayed after verification.