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 2 of 2.

Priya said:   1 decade ago
How indiabix will be printed? They are not calling display function.

Ozlemsen said:   1 decade ago
Agree with @Aakash:

The answer is 4. And it's the size of pointer.

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.

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.

Confidante said:   1 decade ago
The output is coming as 4 only. cout<<sizeof(xx) displays the size of integer only.

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.

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.


Post your comments here:

Your comments will be displayed after verification.