Summary of structures with pointer members

Source: Internet
Author: User
Tags function definition

Structures and pointers are often used in C + +, and when a struct is defined, there is a lot to be noted when there is a pointer in the member of the structure. It is divided into: structure initialization, structure as function parameters, function return value, as well as the structure of the pointer to the situation summarized.

initialization of a struct with a pointer member

When a struct with a pointer member is initialized, the pointer member must be given an explicit address. Note: When initializing a pointer member, or giving it an address, such as an array address, you can manipulate the array by pointer, or the address of a variable, or you can create a new memory, which is new memory, and the address of the memory is determined by the system. An error occurs if the structure containing the pointer operates directly on the pointer before initialization, because the pointer must initialize the operation first.

Here is an example:

struct KeyPoint
{
	int x;
	int y;
	int* data;
	int number;
};

KeyPoint Point;
Point.x = 1;
Point.y = 0;
Point.number = number;
for (int i = 0; i < number; i++)
{
	Point.data[i] = i;  There will be an error, because the pointer is used directly for the initialization of the
You must initialize an assignment for its pointer:

point.data = new int [number];
That's OK.

Two, the structure body containing the pointer member as function parameter

When a structural body containing a pointer member is used as a function parameter, the arguments structure is passed into the function, and the variable of the pointer in the structure variable can be saved, but the change of the general variable cannot be saved. If you want to save changes to a generic variable, write the function argument as a reference type &.

On the basis of the above code:

KeyPoint Point0;
point0.x = ten;
Point0.y = ten;
Point0.number = m;
Point0.data = new Int[point0.number];
for (int j = 0; J < + j)
	point0.data[j] = j;
Changekeypoint (point0); <span style= "White-space:pre" >		</span>//the data pointed to by the run result is changed for each data, But the values of x and y do not change.

<pre name= "code" class= "CPP" >//function definition
void Changekeypoint (KeyPoint keypoints)
{
	keypoints.x + + 1000;
	Keypoints.y + = 1000;
	
	for (int i = 0; i < keypoints.number i++)
	{
		Keypoints.data[i] + = 1000;
	}
}

But if you change the function to:

void Changekeypoint (KeyPoint &keypoints) <span style= "White-space:pre" >	</span>//function parameters are reference types
{
	keypoints.x + = 1000;
	Keypoints.y + = 1000;
	
	for (int i = 0; i < keypoints.number i++)
	{
		Keypoints.data[i] + = 1000;
	}
}
This enables you to change the x and Y values of the struct variables.

third, the structure body containing the pointer member as the function return value

You can return a struct and a generic type of variable (such as int, double, etc.) directly.

KeyPoint point0 = SetPoint (); <span style= "White-space:pre" >		</span>//points the data values of the X, Y, and pointers of the struct body can be returned

function definition
keypoint setpoint ()
{
	KeyPoint point0;
	point0.x = ten;
	Point0.y = ten;
	Point0.number = m;
	Point0.data = new Int[point0.number];
	for (int j = 0; J < + j)
		point0.data[j] = j;
	return point0;
}
Run Result:
point0.x = ten, <pre name= "code" class= "cpp" >POINT0.Y = 10,
<pre name= "code" class= "CPP" >point0.data[j] = j;  J from 0 to 100

Iv. pointers to structures with pointer members

You can define a pointer to a struct body, where the pointer is to a struct, and the pointer holds the pointer to a struct body.

keypoint* points;
points->x = 0;
Points->y = ten;
points->data = new int [100];
It should be noted at this point that the operation of the members of the struct must be done with the "->" operator and not the "." Resolution

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.