Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Passing Structs to the Functions | Pointers and Structs
C Structs
course content

Kursusindhold

C Structs

C Structs

1. Introduction to Structs
2. Pointers and Structs
3. Structs and Memory
4. Advanced Structs Usage
5. Implementing Data Structures

book
Passing Structs to the Functions

Functions can work with structures just like with regular variables:

c

main

copy
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:

c

main

copy
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:

c

main

copy
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; }
Opgave

Swipe to start coding

  1. Provide the correct return type and args of the function
  2. Inside function create a pointer variable and allocate memory for your structure;
  3. Сreate a pointer to store the address returned by the function;
  4. Displaying the info about your person;
  5. Free up the allocated memory.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 5
toggle bottom row

book
Passing Structs to the Functions

Functions can work with structures just like with regular variables:

c

main

copy
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:

c

main

copy
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:

c

main

copy
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; }
Opgave

Swipe to start coding

  1. Provide the correct return type and args of the function
  2. Inside function create a pointer variable and allocate memory for your structure;
  3. Сreate a pointer to store the address returned by the function;
  4. Displaying the info about your person;
  5. Free up the allocated memory.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 5
Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Vi beklager, at noget gik galt. Hvad skete der?
some-alt