66.5k views
1 vote
Use function GetUserInfo to get a user's information. If user enters 20 and Holly, sample program output is:Holly is 20 years old. #include #include void GetUserInfo(int* userAge, char userName[]) { printf("Enter your age: \\"); scanf("%d", userAge); printf("Enter your name: \\"); scanf("%s", userName); return;}int main(void) { int userAge = 0; char userName[30] = ""; /* Your solution goes here */ printf("%s is %d years old.\\", userName, userAge); return 0;}

User Joep
by
4.9k points

1 Answer

6 votes

Answer:

Replace

/*Your solution goes here*/

with

GetUserInfo(&userAge, userName);

And Modify:

printf("%s is %d years old.\\", userName, userAge)

to

printf("%s is %d years old.", &userName, userAge);

Step-by-step explanation:

The written program declares userAge as pointer;

This means that the variable will be passed and returned as a pointer.

To pass the values, we make use of the following line:

GetUserInfo(&userAge, userName);

And to return the value back to main, we make use of the following line

printf("%s is %d years old.", &userName, userAge);

The & in front of &userAge shows that the variable is declared, passed and returned as pointer.

The full program is as follows:

#include <stdio.h>

#include <string.h>

void GetUserInfo(int* userAge, char userName[]) {

printf("Enter your age: ");

scanf("%d", userAge);

printf("Enter your name: ");

scanf("%s", userName);

return;

}

int main(void) {

int* userAge = 0;

char userName[30] = "";

GetUserInfo(&userAge, userName);

printf("%s is %d years old.", &userName, userAge);

return 0;

}

User Ryanrhee
by
5.5k points