Day#45 'C' Coding Challenge

Day#45 'C' Coding Challenge

What is the output of the below code?

#include<stdio.h> struct student { int marks; char *name; }; void main() { struct student roll1 = {56,"Cary"}; struct student roll2 = roll1; printf("%d",roll2.marks); }

--------------------------------------------------------------

The above code tests two scenarios:

1. Structure definition
2. Structure variable
3. Copying a structure to another

--------------------------------------------------------------

roll1 and roll2 are two different structure variables for the same structure. 

struct student roll1 = {56,"Cary"};

The above statement assigns values to the elements within the structure with roll1 variable. If the same contents need to be copied to another structure variable belonging to the same structure, the assignment used shall work.

struct student roll2 = roll1;

So, the output shall be 56 as the roll1 structure copied to roll2 structure.

Note: This may not work if we are trying to copy two different structures.

Post a Comment

0 Comments