Question

C programming Consider the following struct definition: struct player {     char letter;     int age;...

C programming

Consider the following struct definition:

struct player {
    char letter;
    int age;
}
typedef struct player player_t;

Write a program to do the following:

  • Declare a pointer to a player_t variable
  • Allocate memory using malloc
  • Initialise each field of the struct
  • Print the struct inside the main
  • Pass the struct to a function which increments the age by one using a pointer to the struct
  • Print the struct inside the main after the function was called
  • Free the memory allocated to structs, and exit the program
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
#include<stdlib.h>
struct player {
char letter;
int age;
};
typedef struct player player_t;

void fun(player_t *p)
{
p->age = p->age+1;//incrementing age by1
}
int main()
{
player_t *p;//declaring pointer
p = (player_t *)malloc(sizeof(player_t));//allocating memory
//initializing
p->letter='M';
p->age=23;
//printing
printf("%c %d\n",p->letter,p->age);
//passing to function
fun(p);
//printing
printf("%c %d\n",p->letter,p->age);
//freeing memory
free(p);

return 0;
}

output:

M 23
M 24


Process exited normally.
Press any key to continue . . .


//PLS give a thumbs up if you find this helpful, it helps me alot, thanks.


//if you have any doubts, ask me in the comments

Add a comment
Know the answer?
Add Answer to:
C programming Consider the following struct definition: struct player {     char letter;     int age;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT