Pointers to structures in embedded C

Sometimes you might like to manipulate the members of C structures in a more generalized way. This can be done by using pointer reference to structure. Normally it is easier to pass a pointer to a structure than passing the entire structure to function.

Structure pointer can be declared very easily:

struct structure_tag_name *variable;

For instance:

struct 
position{
int x; int y; 
}pos1, *pos2;

we can assign the first structure address to the structure pointer like this:

pos2=&pos1;

now we can access the first structure members by using structure pointer:

pos2->x=10;

or we can write

(*pos2).x=10;

Structures can contain member pointers to other structures or structures of the same type. This way, we can create lists – dynamic structure:

struct list{
int listNo;
struct list *next; //pointer to another structure 
}item1, item2;

This way, you can assign the pointer with the address to another structure:

item1.next=&item2;

Read more about dynamic structures in C tutorials on the internet.

Leave a Reply