Structs

In many higher level languages, you have the ability to create classes. This gives you access to things like inheritance, member functions, and other object oriented principles. C has none of these things. Instead, C has structs, which allow us to group together different data types and store them sequentially in a block of memory. Consider the following example:

#include <stdio.h>

struct point
{
    int x;
    int y;
};

void init_point(struct point * const p, int x, int y);

void print_point(struct point p);

int main()
{
    struct point n;
    
    init_point(&n,1,2);
    
    print_point(n);
    
    return 0;
}

void init_point(struct point * const p, int x, int y)
{
    p->x = x;
    p->y = y;
}

void print_point(struct point p)
{
    printf("(x,y) = (%d,%d)\n",p.x,p.y);
}

You will see this output below:

(x,y) = (1,2)

Let’s start with the declaration of the struct:

struct point
{
    int x;
    int y;
};

The data type is now struct point, and the ints x and y will are called its members. In memory, it will be stored the same as 2 int types that are stored next to each other, but making them part of the struct allows us to group them together as a single data type. Let’s take a look at the first function where we pass a pointer to the struct type:

void init_point(struct point * const p, int x, int y)
{
    p->x = x;
    p->y = y;
}

When you want to access specific members from a pointer to a struct, you will use the -> operator, which will give you a reference to the member type that you wish to modify. The other function takes a struct type, but not a pointer this time:

void print_point(struct point p)
{
    printf("(x,y) = (%d,%d)\n",p.x,p.y);
}

When we are accessing members of a non-pointer struct type, we will use the . operator. We pass the struct by reference using its address when we wish to modify it, and we pass the instance by value when we simply want to access its members, as we can see in our main function:

// declare n
struct point n;

// pass n by reference
init_point(&n,1,2);
    
// pass n by value
print_point(n);

<< prev

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x