Please use C source code to write the following. Note: the following question is
ID: 3720356 • Letter: P
Question
Please use C source code to write the following.
Note: the following question is a follow up to this question: "Using a single line of code, define and initialize a variable “p1” storing a pet with appropriate values.On the next line in the main, create a pointer called “ptrP1“ that points to the pet p1. Next, print thename of the pet to screen using the pointer ptrP1. Print the age of the pet to screen using the variablep1.
int main(){
//define your pet “p1”
//define the pointer “ptrP1”
//print the name using ptrP1
//print the age using p1"
Question 3: Write a function “changeWeight” that takes pet and a new weight as the input to the function.
Hint: Should the pet be passed by value or reference
Explanation / Answer
Question 1:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Pet
{
int age;
char name[5];
};
int main()
{
struct Pet p1, *ptrP1;
p1.age = 10;
strcpy(p1.name, "July");
ptrP1 = &p1;
printf("Name = %s ", (*ptrP1).name);
printf("Age = %d", p1.age);
return 0;
}
Question 3:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Pet
{
int age;
char name[5];
int weight;
};
void changeWeight(struct Pet *ptr, int newValue){
(*ptr).weight = newValue;
}
int main()
{
struct Pet p1;
p1.age = 10;
p1.weight = 10;
strcpy(p1.name, "July");
changeWeight(&p1, 20);
printf("Name = %s ", p1.name);
printf("Age = %d ", p1.age);
printf("Weight = %d", p1.weight);
return 0;
}
**Comment for any further queries.