Struct pointer in C Language

Source: Internet
Author: User

Struct pointer in C Language
1. pointer variable pointing to the struct:
In C language, "->" is a whole. It is used to point to a struct. Suppose we define a struct in the program and declare a pointer variable pointing to this struct, then we need to use a pointer to retrieve the data in the struct, we need to use the pointing operator "-> ".


Example:
Struct SunLL
{
Int;
Int B;
Int c;
};

Struct SunLL * p; // defines the struct pointer.
Struct SunLL A = {1, 2, 3}; // defines A SunLL type variable

Int x; // defines a variable x
P = & A; // point p to
X = p-> a; // equivalent to x = (* p). a (* p) indicates the struct variable pointed to by p.
// The meaning of this sentence is to take out the data item a in the struct pointed to by p and assign it to x
// Because p points to A, p-> a = A. a, that is, 1

2. pointer variable pointing to the struct array:
The pointer variable pointing to the struct can also point to the struct array and its elements.


If the program is as follows:
Struct SunLL * p, sun [3];
P = sun;
If the sun [0] address is assumed to be 1000, the pointer Variable p points to the first address of the structure array sun, because the size of (struct SunLL) value is 6, each struct element occupies 6 bytes of memory space. Therefore, p + 1 points to address 1006, and p + 2 points to address 1012.

When you use pointer variables to point to struct variables or arrays, pay attention to the operator priority. In C language, () []->. the four operators with the same priority have the highest priority, followed by "*" "+ +" "--" "&" operators with the same priority. For example, the expression ++ P-> a is equivalent to ++ (p->)

(++ P)-> a calculate ++ p first, p points to sun [1];
P ++-> a; the expression is equivalent to (p ++)->;
The expression p-> a ++ is equivalent to (p-> a) ++;

3. struct as function parameters and struct pointer as function parameters


Example 1
Struct st
{
Int;
Char B;
};

Fun (struct st bc)
{
Bc. a + = 5;
Bc. B = 'a ';
Printf (% d, % c, bc. a, bc. B );
}

Main ()
{
Struct st bl;
Bl. a = 3;
Bl. B = c;
Fun (bl );
Printf (% d, % c, bl. a, bl. B );
}

Running result: 8, A 3, c

Example 2:
Struct st
{
Int;
Char B;
};

Fun (struct st * bp)
{
Bp-> a + = 5;
Bp-> B = 'a ';
Printf (% d, % c, bc. a, bc. B );
}

Main ()
{
Struct st bl;
Bl. a = 3;
Bl. B = c;
Fun (& bl );
Printf (% d, % c, bl. a, bl. B );
}

Running result: 8, A 8,

Summary:
The struct variable is used as a function parameter to pass the real parameter value to the form parameter, which is a value. All parameters of the struct are passed to the parameter, but their values are not changed. When a pointer pointing to a struct variable is passed as a real parameter, the address of the struct variable is passed to the shape parameter, which is an address transfer that changes the member value of the actual struct variable.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.