C Programming - Structures, Unions, Enums - Discussion

Discussion Forum : Structures, Unions, Enums - Find Output of Program (Q.No. 12)
What will be the output of the program ?

    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
No answer description is available. Let's discuss.
18 comments Page 1 of 2.

Shabnam said:   6 years ago
Thank you @Safi.

Sivasankari said:   6 years ago
Thank you all.

Ramakant Mishra said:   8 years ago
*c is initially point to c[0]. So, *(c+2) is now pointing at c[2].

Manideep innamuri said:   9 years ago
Array index starts from 0 so c[1] indicates to 103, php. So, c[1]. Courseno returns 103 and c always points to index 0 where c+2 in the sense 0+2nd location so c2 location now here we cave c2 values are 104, dotnet, so (* (c+2) ). Course name tends to dotnet finally the answer is 103, dotnet.

Teja said:   1 decade ago
Here in first printf he asked for course number so c[1] is 103 and in next printf he asked for course name so c[2] is dotnet.

Lokesh k.c. said:   1 decade ago
(*(c+2)) where if you give only c that means its index value is 0 so plus 2 i.e. (c+2)=2. Then it will point to "DotNet".

D.NARESH IIIT BASAR 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.

Shoeb said:   1 decade ago
c[1].courseno =102 then c[2].coursename should be php,

So option B,C,D are not correct only Option A is Correct

c[1].courseno =103 then c[2].coursename should be Dotnet,
If we check the option

Satyaprakash said:   1 decade ago
Thank you for Apurva Nigam.

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

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


I thought you cleared.

Post your comments here:

Your comments will be displayed after verification.