Question
Download Solution PDFWhat is the output of following code?
main ()
struct s1
{char * z;
int i;
struct s1 * p;
}
static struct s1 a [ ] = {
{"Nagpur", 1, a + 1}
{"Raipur', 2, a + 2}
{"Kanpur', 3, a}
}:
struct s1* ptr = a:
printf (%s %s %s\n", a[0].z, ptr → z, a[2].p → z);
}
Answer (Detailed Solution Below)
Detailed Solution
Download Solution PDFThe correct answer is Nagpur Nagpur Nagpur
Key PointsIn C programming, a structure (struct) is a user-defined data type that groups related variables of different data types together. A struct is used for packaging data where each element, known as a member, can have a different type.
#include<stdio.h>
typedef struct s1 {
char * z;
int i;
struct s1 * p;
} s1;
int main() {
static s1 a[3] = {{"Nagpur", 1, a + 1}, {"Raipur", 2, a + 2}, {"Kanpur", 3, a}};
s1* ptr = a;
printf("%s %s %s\n", a[0].z, ptr -> z, a[2].p -> z);
return 0;
}
a[0].z refers to the string "Nagpur", which is the z value of the first structure.
ptr -> z also points to "Nagpur" because ptr was initialized to point to the first structure in the array.
a[2].p -> z also gives "Nagpur" because p of the third structure was initialized to point to the first structure in the array.
Therefore, the output of the corrected version of the provided code would be: Nagpur Nagpur Nagpur
This means the correct option from the given choices is 2) Nagpur Nagpur Nagpur.
Last updated on Jun 6, 2025
-> The UGC NET Exam Schedule 2025 for June has been released on its official website.
-> The UGC NET Application Correction Window 2025 is available from 14th May to 15th May 2025.
-> The UGC NET 2025 online application form submission closed on 12th May 2025.
-> The June 2025 Exam will be conducted from 21st June to 30th June 2025
-> The UGC-NET exam takes place for 85 subjects, to determine the eligibility for 'Junior Research Fellowship’ and ‘Assistant Professor’ posts, as well as for PhD. admissions.
-> The exam is conducted bi-annually - in June and December cycles.
-> The exam comprises two papers - Paper I and Paper II. Paper I consists of 50 questions and Paper II consists of 100 questions.
-> The candidates who are preparing for the exam can check the UGC NET Previous Year Papers and UGC NET Test Series to boost their preparations.