Course Object Oriented Programming

Pointers to Structs

Course's Page 10 min Text by
User Image
Allan Garcia

Now we will learn how to access information in three different situations: from variables, from memory positions and from pointers. We will also study the use of the latter for the creation of dynamic struct vectors.

Accessing Information from a Variable

As we have seen before, we use the "." operator to access the information contained in a struct type variable.

typedef struct pair { // Declaring a struct pair with the synonym pair
    int x;
    int y;
} pair;

int main() {
    pair P; // Declaring the variable P of the type struct pair
    P.x = 1; // Accessing variable x contained in variable P
    P.y = 2; // Accessing variable y contained in variable P
}

Accessing information from a memory location