Struct pointer Summary

Source: Internet
Author: User

Struct pointer

Pointer as function parameter

Array as function parameter

1. struct pointer

Pointer to a struct variable. It is called a struct pointer.

Student Stu = {0 };

Student * P = & Stu;

Student * struct pointer type (type), P struct pointer variable (variable name );

& Stu struct variable address (initial value)

(* P). Sex = 'M ';

P-> sex = 'M ';

(* P) after the pointer value is a struct variable, sex is the access member variable.

The pointer can be used to direct the operator to access the struct member P-> sex.

1. struct pointer

The struct pointer variable stores the first address of the struct variable. Its essence is the address of the first member variable of the struct.

You can use struct pointer variables and point operators to access each member variable.

For a struct, the number of bytes occupied by the defined struct depends not only on the Data Type of its members, but also on the member sequence. The size of space occupied by different sequences may vary.

For pointer variables, the auto-increment operation is performed. The number of bytes occupied by each pointer offset for a corresponding data type. When the content operator is used, the data in the memory range is also determined based on the address saved by the pointer variable and the Data Type of the pointer variable from the address to the number of bytes. It is also the reason why pointer variables are essentially the first address of a storage area. The array name of the struct array is a struct pointer constant.

Pointer contained in the Structure

When defining a struct, you can use the pointer variable as a member of the struct, but you must prevent the struct type variables from being defined, and the data corresponding to the pointer variable is a constant. If it is a constant, the modification will crash.

2. pointer as a function parameter

Pointers can be used as function parameters.

Including: Generally, pointers are used as function parameters, arrays are used as function parameters, and struct pointers are used as function parameters.

Transfer Value and address

Passing a value is to pass the value of the real parameter to the form parameter.

The so-called address transfer, we sometimes need to change the value of the real parameter, then we need to transfer the address.

Use the address as a function parameter to pass in the function.

3. array as function parameter

The array is used as a function parameter, and the form parameter only represents the first address of the array. We need to input the number of array elements.


Struct pointer Summary

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.