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

Contenido del Curso

C Structs

C Structs

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

bookPointers to Structs

The C programming language allows you to create pointers not only to regular data types but also to structures (since a structure is a custom data type).

Declaration and initialization of a pointer looks like this:

If <name_of_pointer> is a pointer to a structure,
then *(<name_of_pointer>) is the structure itself.

To access the fields of a structure through a pointer, you must use the -> operator.

Note

"->" is an analogue of the structure access operator .

c

main

copy
12345678910111213141516171819202122
#include <stdio.h> // structure declaration struct Point { int x; int y; }; int main() { // create a structure variable of Point type struct Point p1; p1.x = 10; p1.y = 20; // create a pointer to a structure of type Point and assign it the address of variable p1 struct Point* ptr = &p1; // Access structure members via pointer by `->` operator printf("Coordinates: (%d, %d)\n", ptr->x, ptr->y); return 0; }

Tarea

  1. Declare structure Human which contains two fields name and age;
  2. Initialize the structure worker and fill the fields;
  3. Declare and initialize a pointer ptr to a worker;
  4. Display information about the worker.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 2
toggle bottom row

bookPointers to Structs

The C programming language allows you to create pointers not only to regular data types but also to structures (since a structure is a custom data type).

Declaration and initialization of a pointer looks like this:

If <name_of_pointer> is a pointer to a structure,
then *(<name_of_pointer>) is the structure itself.

To access the fields of a structure through a pointer, you must use the -> operator.

Note

"->" is an analogue of the structure access operator .

c

main

copy
12345678910111213141516171819202122
#include <stdio.h> // structure declaration struct Point { int x; int y; }; int main() { // create a structure variable of Point type struct Point p1; p1.x = 10; p1.y = 20; // create a pointer to a structure of type Point and assign it the address of variable p1 struct Point* ptr = &p1; // Access structure members via pointer by `->` operator printf("Coordinates: (%d, %d)\n", ptr->x, ptr->y); return 0; }

Tarea

  1. Declare structure Human which contains two fields name and age;
  2. Initialize the structure worker and fill the fields;
  3. Declare and initialize a pointer ptr to a worker;
  4. Display information about the worker.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 2
toggle bottom row

bookPointers to Structs

The C programming language allows you to create pointers not only to regular data types but also to structures (since a structure is a custom data type).

Declaration and initialization of a pointer looks like this:

If <name_of_pointer> is a pointer to a structure,
then *(<name_of_pointer>) is the structure itself.

To access the fields of a structure through a pointer, you must use the -> operator.

Note

"->" is an analogue of the structure access operator .

c

main

copy
12345678910111213141516171819202122
#include <stdio.h> // structure declaration struct Point { int x; int y; }; int main() { // create a structure variable of Point type struct Point p1; p1.x = 10; p1.y = 20; // create a pointer to a structure of type Point and assign it the address of variable p1 struct Point* ptr = &p1; // Access structure members via pointer by `->` operator printf("Coordinates: (%d, %d)\n", ptr->x, ptr->y); return 0; }

Tarea

  1. Declare structure Human which contains two fields name and age;
  2. Initialize the structure worker and fill the fields;
  3. Declare and initialize a pointer ptr to a worker;
  4. Display information about the worker.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

The C programming language allows you to create pointers not only to regular data types but also to structures (since a structure is a custom data type).

Declaration and initialization of a pointer looks like this:

If <name_of_pointer> is a pointer to a structure,
then *(<name_of_pointer>) is the structure itself.

To access the fields of a structure through a pointer, you must use the -> operator.

Note

"->" is an analogue of the structure access operator .

c

main

copy
12345678910111213141516171819202122
#include <stdio.h> // structure declaration struct Point { int x; int y; }; int main() { // create a structure variable of Point type struct Point p1; p1.x = 10; p1.y = 20; // create a pointer to a structure of type Point and assign it the address of variable p1 struct Point* ptr = &p1; // Access structure members via pointer by `->` operator printf("Coordinates: (%d, %d)\n", ptr->x, ptr->y); return 0; }

Tarea

  1. Declare structure Human which contains two fields name and age;
  2. Initialize the structure worker and fill the fields;
  3. Declare and initialize a pointer ptr to a worker;
  4. Display information about the worker.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 2. Capítulo 2
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
some-alt