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;
}
103 DotNet
102 Java
103 PHP
104 DotNet
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
18 comments Page 2 of 2.

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.

Sravanthi Emmadi said:   1 decade ago
Thank you Apurva.

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 :)

Prits said:   1 decade ago
What is use of pointer here? any special use? will anyone explain that please.

Thanks.

Iram said:   1 decade ago
Anyone can explain in detail.

Jitu Rahangdale said:   1 decade ago
Ok, pravin said is right. I agree with him.

Sivaram said:   1 decade ago
Thanks praveen.

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.


Post your comments here:

Your comments will be displayed after verification.