To share this page click on the buttons below;
Pointer to structures
This very short chapter deal with some very simple peculiarities that affect pointers and structures.
Remember that a structure is a sort of new type of variable (a composite one) that is built grouping together two or more different variables.
The structure variables are the fields of the structure. Once you have your structure you can access the fields using the . (dot) operator. So name of the structure.name of the field is the way you get access to the fields of the structure.
There is nothing strange in structure pointer notation, as usually you can declare a pointer to a struct using the * operator and you can get the address of a struct variable using the operator &.
The peculiarity with pointers to structure happens when, given a pointer to a certain struct variable, it is necessary to access the fields of the struct through the pointer.
In this case, instead of using the "dot" operator you use the operator –> (sort of arrow): the pointer–>the name of the field gives access to structure field via pointer.
The following example shows these concepts:
#include <stdio.h>
/* program to demonstrate pointer to structures */
/* a struct to group together 2 cartesian coordinates */
struct tagPoint {
unsigned int x;
unsigned int y;
};
/* a Rectangle structure made up by two point structure */
struct tagRect {
struct tagPoint top_left;
struct tagPoint bottom_right;
};
void move_rect_up(struct tagRect *rc);
void move_rect_down(struct tagRect *rc);
void print_rect(struct tagRect rc);
void print_rect(struct tagRect rc){
printf("% 2i,% 2i ----------------- |\n",rc.top_left.x, rc.top_left.y);
printf(" | |\n");
printf(" | |\n");
printf(" | ---------------- % 2i,% 2i\n\n\n\n", rc.bottom_right.x, rc.bottom_right.y);
}
void move_rect_up(struct tagRect *rc) {
rc->top_left.y--;
rc->bottom_right.y--;
}
void move_rect_down(struct tagRect *rc) {
rc->top_left.y++;
rc->bottom_right.y++;
}
void main(void) {
printf("PROGRAM to demonstrate pointer to STRUCTURES\n");
/* declaration of a variable rc "of type" struct tagRect */
struct tagRect rect = { 5, 2, 10, 7};
struct tagPoint *pt;
struct tagRect *ptr_rc;
print_rect(rect);
move_rect_up(&rect);
print_rect(rect);
move_rect_down(&rect);
print_rect(rect);
ptr_rc = ▭
pt = &(rect.bottom_right);
printf("Rectangle bottom right corner %i, %i\n", pt->x, pt->y);
pt = &(ptr_rc->top_left);
printf("Rectangle top left corner %i, %i\n", pt->x, pt->y);
}
That is a very simple program and it uses the structures of Point and Rectangle as we defined in the structure chapter, but probably a couple of things are worth to note.
This program tries to draw squares (see print_rect
function) in a very
rough way. To understand the notation it is probably necessary to explain that in
computer vision the coordinates of the points on a screen start
from the top left corner (0,0), the x-es grow towards right and the y-es grow towards the bottom of the screen.
Another point that might be interesting to notice is how the fields of the point structure are accessed via pointer in the functions move_rect_up
and move_rect_down
: the argument rc
is a pointer to a Rectangle structure (that contains two further Point structures), at first the "arrow" operator is used to reach the Point structure, then the "dot" operation is used to finally get the y variable member of the Point structure. When the operator "arrow" is used starting from the rc
pointer what we get is the Point struct
variable member of the rectangle structure, that is no more a pointer but a variable and so the only way to get the y field is using the "dot" operator.
In the last 4 rows of the main I show you some particularities of the notation: if we define a pointer to the Point structure (something like struct tagPoint *pt;
) we can get the address of the point structure inside the rectangle using the rect
variable, accessing the point structure through the "dot" operator and getting its address (&(rect.bottom_right)
) or, having a pointer to the rect
variable like ptr_rc
) we can first reach the point structure via the "arrow" operator and then get the address of this sub-structure ( &(ptr_rc->top_left);
).
Note finally that, being pt
a pointer, to access the fields x and y you need the "arrow" operator and no more the "dot" one.
To share this page click on the buttons below;