C Programming - Structures, Unions, Enums - Discussion
Discussion Forum : Structures, Unions, Enums - Find Output of Program (Q.No. 12)
12.
What will be the output of the program ?
#include<stdio.h>
struct course
{
int courseno;
char coursename[25];
};
int main()
{
struct course c[] = { {102, "Java"},
{103, "PHP"},
{104, "DotNet"} };
printf("%d ", c[1].courseno);
printf("%s\n", (*(c+2)).coursename);
return 0;
}
Discussion:
18 comments Page 1 of 2.
Praveen said:
1 decade ago
When you do c[1].courseno it will print the 'c[1]' array 1st variable ie 103 then (*(c+2)).coursename it will go into c[2] array's coursename contents.
Sivaram said:
1 decade ago
Thanks praveen.
Jitu Rahangdale said:
1 decade ago
Ok, pravin said is right. I agree with him.
Iram said:
1 decade ago
Anyone can explain in detail.
Prits said:
1 decade ago
What is use of pointer here? any special use? will anyone explain that please.
Thanks.
Thanks.
Apurva Nigam said:
1 decade ago
The c[1].courseno points to 103 ( hope you agreed).
In (*(c+2)).coursename , *(c+2) is same as writing c[2] that is c[2] or *(c+2) points to {104, "DotNet"}
Therefore (*(c+2)).coursename or u can say c[2].coursename will give DotNet.
Take care :)
In (*(c+2)).coursename , *(c+2) is same as writing c[2] that is c[2] or *(c+2) points to {104, "DotNet"}
Therefore (*(c+2)).coursename or u can say c[2].coursename will give DotNet.
Take care :)
Sravanthi Emmadi said:
1 decade ago
Thank you Apurva.
Ramdas said:
1 decade ago
When we refer c[1], it returns 103.
And when we are using (* (c+2) ). Coursename) it refers to c[2]. Coursename because c itself points to starting address of array c. C+2 is refers to c[2]address. For getting the value in that location used * deference operator.
And when we are using (* (c+2) ). Coursename) it refers to c[2]. Coursename because c itself points to starting address of array c. C+2 is refers to c[2]address. For getting the value in that location used * deference operator.
Safi said:
1 decade ago
The struct is a user defined data type,means defines by user for avoid multiple declaration in the program body.this data type is accessed by a Dot "." operator. Fine......
Here struct for "course" variable. Now
printf("%d", c[1].courseno);
will return courseno stored at 2nd array location of c,ie c[1] bcz by default array initiates with index 0 and it is 1st location of that array...while return type in printf function is integer.....
and
printf("%d", c[1].courseno);
ans= 103
we can write c[2] as similar to (*(c+2)) and vise versa.
hence using above funda
printf("%s\n", (*(c+2)).coursename);
return type in printf function is string, therefore
it gives answer
DotNet
I thought you cleared.
Here struct for "course" variable. Now
printf("%d", c[1].courseno);
will return courseno stored at 2nd array location of c,ie c[1] bcz by default array initiates with index 0 and it is 1st location of that array...while return type in printf function is integer.....
and
printf("%d", c[1].courseno);
ans= 103
we can write c[2] as similar to (*(c+2)) and vise versa.
hence using above funda
printf("%s\n", (*(c+2)).coursename);
return type in printf function is string, therefore
it gives answer
DotNet
I thought you cleared.
Satyaprakash said:
1 decade ago
Thank you for Apurva Nigam.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers