C pointers and structures

We covered C structures before, and in this lesson, we go through C pointers and structures. In C language, it is possible to define a structure type pointer, just like defining other types of pointers. 

Defining structure pointer

Defining structure pointers is the same as defining structure variables, except that the variable name is preceded by a *. Consider the following example: 

struct bal{
    float balance;
    char name[80];  
}person, *p;

The person is a structure variable, and p is a structure pointer. Now consider the following expression:

p = &person;

The address of the person structure is assigned to the p pointer.

Accessing structure with pointer

To access the contents of the structure’s elements with a pointer, the pointer must be enclosed in parentheses. Pay attention to the following code:

(*p).balance;
  1. The above expression accesses the balance element of the person structure.
  2. The reason for putting the pointer variable in parentheses is that the (.) operator has precedence over *. 

In general, two methods could be used to access the structure’s elements pointed to by a pointer:

  1. Mention the name of the pointer inside the parentheses and then the names of the desired elements separated by dots. (such as accessing the balance element of the person structure by the pointer p)
  2. Using the “->” operator, which is a more appropriate method. If you want to use the -> operator to access the balance element of the person structure, you must do the following:
p -> balance;

Precedence of operators

Here it is necessary to state that arrays, pointers, and structures are closely related. Meanwhile, their related operators, including subscript or [ ], structure member or dot, and indirect access to structure member or ->, all have the highest precedence.

Indirect access or * and address extraction or & operators have second priority. If these appear together in an expression, the desired code should be written a little carefully. Pay attention to the following example:

struct point{
    float x;
    float y;
};

struct line{
    struct point first;  
    struct point last;  
    char name  
}a = {{1, 1},{10, 20}, 'a'};

struct line m[] = { { {2, 3}, {4, 5}, 'c'}, 
                    { {4, 6}, {8, 1}, 'm'}, 
                    { {8, 5}, {4, 2}, 'x'}};

struct line *pa = &a;
struct line *pm = &m[1];
  1. On lines 1 to 4, a structure named point is defined, but no variable is defined for it.
  2. On lines 6 to 10, a structure named Line is defined, and a variable named a is defined and initialized.
  3. The first two elements of the line structure are the point structure. So the elements of a structure can be structures.
  4. On line 12, a structure variable for the line is defined and assigned a value. Here, the variable m is an array of structures.
  5. On line 16, the address of the variable a is assigned to the pointer pa.
  6. On line 17, the address of the second element of the array m is assigned to the pointer pm.

It’s the time to access the structures’ members:

a.first.x

pa->first.x

(a.first).x

(pa->first).x

(*pa).first.x
  1. Line one: a is a variable of the line structure type, and its first member is accessed by a dot.
  2. First is also a structure point type variable, whose member x is accessed by a dot.
  3. Line two: pa is a pointer to the structure variable a. So using -> operator, we can access its members.
  4. Interpret the other lines according to the explanations given. If you have any questions, be sure to ask in the comments section.

The following two expressions also have the same meaning:

m[1].last.y

pm->last.y
  1. On line one: the last member and then the y member have been accessed through the index of the first element of the array m, which is a structure.
  2. In the second line: access to the last member is done through the address of the first element of the array m, which is located in the pm variable.
Was this helpful?
[0]
Scroll to Top
Scroll to Top