#include<stdio.h>#include<string.h>struct player { char pname[20];}pl;char* play(struct player *temp_pl){ strcpy(temp_pl->pname, "kohli"); return temp_pl->pname;}int main(){ strcpy(pl.pname, "dhoni"); printf("%s %s", pl.pname, play(&pl)); return 0;}
Question
#include<stdio.h>#include<string.h>struct player { char pname[20];}pl;char* play(struct player *temp_pl){ strcpy(temp_pl->pname, "kohli"); return temp_pl->pname;}int main(){ strcpy(pl.pname, "dhoni"); printf("%s %s", pl.pname, play(&pl)); return 0;}
Solution
This is a C program that uses a structure to store player names and a function to change the player name. Here's a step-by-step explanation:
-
The program starts with the inclusion of two header files:
stdio.hfor standard input/output functions andstring.hfor string manipulation functions. -
A structure named
playeris defined with a single memberpnamewhich is an array of characters (a string). -
An instance of the
playerstructure namedplis declared globally. -
A function named
playis defined which takes a pointer to aplayerstructure as an argument. Inside this function, thepnameof the passed structure is set to "kohli" using thestrcpyfunction. The function then returns thepnameof the passed structure. -
The
mainfunction is where the program execution starts. Insidemain, thepnameof the globalplstructure is set to "dhoni" usingstrcpy. -
The
printffunction is then used to print thepnameofpland the result of theplayfunction when passed a pointer topl. Since theplayfunction changes thepnameofplto "kohli", the output of the program will be "kohli kohli". -
The
mainfunction returns 0 to indicate successful execution of the program.
Similar Questions
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.