Passing Structs to the Functions
Functions can work with structures just like with regular variables:
main.c
123456789101112131415161718192021#include <stdio.h> // structure definition typedef struct { char name[50]; } Person; // function to display information about a person void printPerson(Person p) { printf("Name: %s\n", p.name); } int main() { // creating a structure and initializing its values Person person1 = {"John"}; // call a function to display information about a person printPerson(person1); return 0; }
In order for a function to "interact" with a structure, for example, change the fields of an existing structure, the function must accept a pointer to the structure:
main.c
12345678910111213141516171819202122232425262728293031#include <stdio.h> // structure definition typedef struct { char symbol; }Example; // function for changing the values of structure fields via a pointer void changePoint(Example* ptr, int newSymbol) { // check for NULL pointer if (ptr != NULL) { ptr->symbol = newSymbol; } } int main() { // create the Example structure and a pointer Example ptr1 = {'H'}; Example* ptr = &ptr1; printf("Old symbol: %c | %p\n", ptr1.symbol, &ptr1); // use function to change the field of structures changePoint(ptr, 'y'); printf("New symbol: %c | %p\n", ptr1.symbol, &ptr1); return 0; }
Structures can be created inside functions, and such structures can "live" outside the functions (not locally) if the function returns a pointer to such a structure:
main.c
1234567891011121314151617181920212223242526272829303132#include <stdio.h> #include <stdlib.h> // structure definition typedef struct { int value; }Example; // function creates a structure with the given field Example* CreateStruct(int setVal) { Example* ptr = (Example*)malloc(sizeof(Example)); // check for successful memory allocation if (ptr != NULL) { ptr->value = setVal; } return ptr; } int main() { // use function to create structure Example* ptrToStruct = CreateStruct(23); printf("Value inside struct: %d", ptrToStruct->value); free(ptrToStruct); // free memory return 0; }
Swipe to start coding
You are managing a small team of three employees, and each one has an hourly rate and the number of hours worked per week.
Your task is to calculate the average weekly salary for the entire team.
Inside the calculateWeeklySalary function:
- Receive a structure of type
Employeeas a parameter. - Access the fields
hourlyRateandhoursWorked. - Multiply them to calculate the weekly salary for that employee.
- Return the calculated salary as a
double.
Inside the calculateAverageSalary function:
- Receive three
Employeestructures as parameters. - Call
calculateWeeklySalary()for each employee to get their total pay. - Add all three salaries together.
- Divide the total by
3.0to get the average weekly salary. - Return this average as a
double.
Example
| Employee Name | Hourly Rate | Hours Worked | Weekly Salary |
|---|---|---|---|
| Alice Johnson | 22.5 | 40 | 900.00 |
| Bob Smith | 18.0 | 38 | 684.00 |
| Carol White | 25.0 | 42 | 1050.00 |
Average Weekly Salary: 878.00 💰
Рішення
Дякуємо за ваш відгук!
single
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Awesome!
Completion rate improved to 4.17
Passing Structs to the Functions
Свайпніть щоб показати меню
Functions can work with structures just like with regular variables:
main.c
123456789101112131415161718192021#include <stdio.h> // structure definition typedef struct { char name[50]; } Person; // function to display information about a person void printPerson(Person p) { printf("Name: %s\n", p.name); } int main() { // creating a structure and initializing its values Person person1 = {"John"}; // call a function to display information about a person printPerson(person1); return 0; }
In order for a function to "interact" with a structure, for example, change the fields of an existing structure, the function must accept a pointer to the structure:
main.c
12345678910111213141516171819202122232425262728293031#include <stdio.h> // structure definition typedef struct { char symbol; }Example; // function for changing the values of structure fields via a pointer void changePoint(Example* ptr, int newSymbol) { // check for NULL pointer if (ptr != NULL) { ptr->symbol = newSymbol; } } int main() { // create the Example structure and a pointer Example ptr1 = {'H'}; Example* ptr = &ptr1; printf("Old symbol: %c | %p\n", ptr1.symbol, &ptr1); // use function to change the field of structures changePoint(ptr, 'y'); printf("New symbol: %c | %p\n", ptr1.symbol, &ptr1); return 0; }
Structures can be created inside functions, and such structures can "live" outside the functions (not locally) if the function returns a pointer to such a structure:
main.c
1234567891011121314151617181920212223242526272829303132#include <stdio.h> #include <stdlib.h> // structure definition typedef struct { int value; }Example; // function creates a structure with the given field Example* CreateStruct(int setVal) { Example* ptr = (Example*)malloc(sizeof(Example)); // check for successful memory allocation if (ptr != NULL) { ptr->value = setVal; } return ptr; } int main() { // use function to create structure Example* ptrToStruct = CreateStruct(23); printf("Value inside struct: %d", ptrToStruct->value); free(ptrToStruct); // free memory return 0; }
Swipe to start coding
You are managing a small team of three employees, and each one has an hourly rate and the number of hours worked per week.
Your task is to calculate the average weekly salary for the entire team.
Inside the calculateWeeklySalary function:
- Receive a structure of type
Employeeas a parameter. - Access the fields
hourlyRateandhoursWorked. - Multiply them to calculate the weekly salary for that employee.
- Return the calculated salary as a
double.
Inside the calculateAverageSalary function:
- Receive three
Employeestructures as parameters. - Call
calculateWeeklySalary()for each employee to get their total pay. - Add all three salaries together.
- Divide the total by
3.0to get the average weekly salary. - Return this average as a
double.
Example
| Employee Name | Hourly Rate | Hours Worked | Weekly Salary |
|---|---|---|---|
| Alice Johnson | 22.5 | 40 | 900.00 |
| Bob Smith | 18.0 | 38 | 684.00 |
| Carol White | 25.0 | 42 | 1050.00 |
Average Weekly Salary: 878.00 💰
Рішення
Дякуємо за ваш відгук!
single