1. Pointer variable pointing to struct body:
C is a whole, it is used to point to the structure, if we define a struct in the program, and then declare a pointer variable to the struct, then we have to use the pointer to the structure of the data, we need to use the pointer operator "."
To illustrate:
struct SUNLL
{
int A;
int b;
int C;
};
struct SUNLL * p; Defining struct-body pointers
struct SUNLL A = {n/A}; Defines a SUNLL type of variable a
int x; Define a variable X
p = &a; Let P point to a
x = p->a; Equivalent to x= (*p). A (*p) represents the structural body variable p points to
The meaning of this sentence is to remove the data item A in the struct that P points to is assigned to X
Because P points to a at this time, so p->a = = A.A, that is, 1
2. Pointer variable to struct array:
Pointer variables that point to a struct can also point to a struct array and its elements.
If the program is as follows:
struct SUNLL *p,sun[3];
p = Sun;
If the address of sun[0] is assumed to be 1000, the pointer variable p points to the first address of the struct array sun, because the value of size of (struct SUNLL) is 6, each struct element occupies 6 bytes of memory space, so p+1 points to address 1006,p+2 to address 1012.
When you use pointer variables to point to a struct or struct array, you should be aware of the precedence of the operators in the C language "()" "[]" "," "." The four priorities are the same, with the highest priority, followed by "*" "+ +" "--" "&" four operators of the same precedence. " Example: ++p->a expression is equivalent to + + (P->a)
(++p)->a first calculates ++p,p point sun[1];
The p++->a; expression is equivalent to (p++)->a;
The expression of p->a++ is equivalent to (p->a) + +;
3, struct as function parameter and structure pointer as function parameter
Example 1
Struct ST
{
int A;
Char b;
};
Fun (struct st BC)
{
bc.a+=5;
bc.b= ' A ';
printf ("%d,%c\n", bc.a,bc.b);
}
Main ()
{
struct St BL;
Bl.a=3;
Bl.b= "C";
Fun (BL);
printf ("%d,%c\n", bl.a,bl.b);
}
Operation Result: 8,a 3,c
Example 2:
Struct ST
{
int A;
Char b;
};
Fun (struct St *BP)
{
bp->a+=5;
bp->b= ' A ';
printf ("%d,%c\n", bc.a,bc.b);
}
Main ()
{
struct St BL;
Bl.a=3;
Bl.b= "C";
Fun (&BL);
printf ("%d,%c\n", bl.a,bl.b);
}
Operation Result: 8,a 8,a
Summarize:
The struct variable is used as a function parameter, which is passed to the formal parameter, which is the value pass. All parameters of the struct are passed to the parameter for use, but the value itself does not change. When you pass a pointer to a struct variable as an argument, you pass the address of the struct variable to the parameter, which is the address pass, which changes the member value of the actual struct variable.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
struct pointer in C language