This section contains the C++ find output programs with their explanations on C++ Structures (set 1).
Program 1:
#include <iostream>
#include <math.h>
using namespace std;
struct st {
int A = NULL;
int B = abs(EOF + EOF);
} S;
int main()
{
cout << S.A << " " << S.B;
return 0;
}
Program 2:
#include <iostream>
using namespace std;
typedef struct{
int A = 10;
int B = 20;
} S;
int main()
{
cout << S.A << " " << S.B;
return 0;
}
Program 3:
#include <iostream>
using namespace std;
typedef struct{
int A;
char* STR;
} S;
int main()
{
S ob = { 10, "india" };
S* ptr;
ptr = &ob;
cout << ptr->A << " " << ptr->STR;
return 0;
}
Answer Program 1:
Output:
Explanation:
Here, we created a structure that contains two integer variables. Here, we used NULL, EOF, and abs() function for initialization of structure elements.
The values of NULL and EOF are 0 and -1 respectively. And the function abs() returns positive value always.
A = NULL; //that is 0 A = 0; B = abs(EOF+EOF); = abs(-1+-1); = abs(-1-1); = abs(-2); = 2;
Then, the final value 0 and 2 will be printed on the console screen.
Answer Program 2:
Output:
Explanation:
Here, we created a structure using typedef that contains two integer variables A and B. Consider the below statement,
cout <<S.A<<" "<<S.B;
Here, we accessed A using S. but S is not an object or variable of structure, we used typedef it means S is type, so we need to create an object of structure using S like, S ob;
Then, ob.A and ob.B will be the proper way to access A and B.
Answer Program 3:
Output:
Explanation:
Here, we created a structure with two members A and STR. In the main() function, we created the object that is ob, and a pointer ptr and then assigned the address of ob to ptr. Accessing the elements using referential operator -> and then printed them on the console screen.
The final output "10 india" will be printed on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer