[Question] If a C++ structure (array) pointer is used as a function parameter, will the value of the actual parameter be modified?

The C++ structure pointer is used as a function parameter, and the value of the actual parameter will not be modified?

foreword

I recently learned C++ pointers and structures. I remember that when a function passes parameters, if the formal parameters pass pointers, the value of the actual parameter can be modified.

But today I saw an array of structures passed into a function as a formal parameter, but the value of the actual parameter was successfully modified. I feel very doubtful?

example

#include<iostream>
using namespace std;

//英雄结构体
struct hero
{
    
    
	string name;
	int age;
	string sex;
};

void changeArray(hero arr[])
{
    
    
	arr[0].name = "猪猪侠";
}

//打印数组
void printHeros(hero arr[], int len)
{
    
    
	for (int i = 0; i < len; i++)
	{
    
    
		cout << "姓名: " << arr[i].name << " 性别: " << arr[i].sex << " 年龄: " << arr[i].age << endl;
	}
}

int main() {
    
    

	struct hero arr[5] =
	{
    
    
		{
    
    "刘备",23,"男"},
		{
    
    "关羽",22,"男"},
		{
    
    "张飞",20,"男"},
		{
    
    "赵云",21,"男"},
		{
    
    "貂蝉",19,"女"},
	};
	
	 //获取数组元素个数
	int len = sizeof(arr) / sizeof(hero);

	printHeros(arr, len); 
	
	changeArray(arr);
	
	cout << "=============" << endl;
	
	printHeros(arr, len);
	
	system("pause");

	return 0;
}
姓名: 刘备 性别: 男 年龄: 23
姓名: 关羽 性别: 男 年龄: 22
姓名: 张飞 性别: 男 年龄: 20
姓名: 赵云 性别: 男 年龄: 21
姓名: 貂蝉 性别: 女 年龄: 19
=============
姓名: 猪猪侠 性别: 男 年龄: 23
姓名: 关羽 性别: 男 年龄: 22
姓名: 张飞 性别: 男 年龄: 20
姓名: 赵云 性别: 男 年龄: 21
姓名: 貂蝉 性别: 女 年龄: 19

reason

When writing C++, when an array is passed in as a function parameter, the array is automatically converted into a pointer variable.

#include<iostream>
using namespace std;

void printArr(int arr[])
{
    
    
	cout << *arr << endl;
	arr++; // 正确
	cout << *arr << endl;
	
	cout << arr[1] << endl;
	arr[1] = 0;
	cout << arr[1] << endl;
};

int main() {
    
    

	int arr[] = {
    
    1,2,3}; // arr++; 这里错误
	
	printArr(arr);
	
	system("pause");

	return 0;
}

1
2
3
0

Therefore, when the formal parameter group is modified inside the function, the actual parameters will also change.

yet another

I recently learned C++ pointers and structures. I remember that when a function passes parameters, if the formal parameters pass pointers, the value of the actual parameter can be modified.

But today I saw a question about passing a structure into a function as a pointer, but failed to modify the value of the actual parameter.

#include<iostream>
using namespace std;

//英雄结构体
struct hero
{
    
    
	string name;
	int age;
	string sex;
};

void printArr(int arr[])
{
    
    
	cout << *arr << endl;
	arr++; // 正确
	cout << *arr << endl;
	
	cout << arr[1] << endl;
	arr[1] = 0;
	cout << arr[1] << endl;
};

void show(hero p) {
    
    
	cout << "结构体形参传递:" << endl; 
	cout << p.name << endl;	
	p.name = "猪猪侠";
	cout << p.name << endl;	
}
void show2(hero * p) {
    
    
	cout << "结构体指针传递:" << endl; 
	cout << p->name << endl;	
	p->name = "猪猪侠";
	cout << p->name << endl;	
}

int main() {
    
    

	int arr[] = {
    
    1,2,3}; // arr++; 这里错误
	
	printArr(arr);
	
	struct hero p = {
    
    "刘备",23,"男"};
	
	show(p);  // 刘备 
	cout << "结构体实参:" << endl; 
	cout << p.name << endl;	// 实参未变 
	
	// 报错 
	show2(&p);  // 刘备 
	cout << "结构体实参:" << endl; 
	cout << p.name << endl;	// 实参未变 
	
	system("pause");
	
	return 0;
}
1
2
3
0
结构体形参传递:
刘备
猪猪侠
结构体实参:
刘备
结构体指针传递:
刘备
猪猪侠
结构体实参:
猪猪侠

But this article does not understand the problem that the structure pointer in C++ is used as a function parameter and will not modify the value of the actual parameter ! My test should be able to modify the value of the actual parameter.

To be studied!

Reference : Link


come on!

grateful!

effort!

Guess you like

Origin blog.csdn.net/qq_46092061/article/details/121480302