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
- Provide the correct return type and args of the function
- Inside function create a pointer variable and allocate memory for your structure;
- Сreate a pointer to store the address returned by the function;
- Displaying the info about your person;
- Free up the allocated memory.
Solución
¡Gracias por tus comentarios!
single
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Resumir este capítulo
Explicar el código en file
Explicar por qué file no resuelve la tarea
Awesome!
Completion rate improved to 4.17
Passing Structs to the Functions
Desliza para mostrar el menú
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
- Provide the correct return type and args of the function
- Inside function create a pointer variable and allocate memory for your structure;
- Сreate a pointer to store the address returned by the function;
- Displaying the info about your person;
- Free up the allocated memory.
Solución
¡Gracias por tus comentarios!
Awesome!
Completion rate improved to 4.17single