What is the difference between the * sign close to the data type and the variable name, and the & sign close to the data type and the variable name?


C++ pointers

Pointer is a variable whose value is the address of another variable

(1) Define pointers

type *var-name;
// 一个整型的指针
int    *ip;  

(2) Use pointers

指针前加一个 * 代表解引用,来取指针所指向地址 的 变量的值
#include <iostream>
 
using namespace std;
 
int main ()
{
   int  a = 20;   // 定义变量
   int  *ip;      // 1、定义指针
 
   ip = &var;       // 在指针变量中存储 a 的地址
 
   cout << "变量a的值: ";
   cout << a << endl;
 
   // 输出在指针变量中存储的地址
   cout << "输出在指针变量中存储的地址: ";
   cout << ip << endl;
 
   // 2、使用指针,解引用
   cout << "取得指针所指向地址 的 变量的值: ";
   cout << *ip <<   endl;
 
   return 0;
}
1. Int* age and int *age, what is the difference between the * sign close to the data type and the variable name:

In C and similar programming languages, int* age and int *age both declare a pointer variable pointing to an integer (int) type, but they are The syntax is slightly different, and there are some subtle effects when it comes to the declaration and parsing of pointer variables.

1. int* age:

In this notation, int* is treated as a whole, meaning "pointer to integer". This syntax emphasizes that variable age is a pointer to an integer type. You can access integer values ​​through age pointers.

int* age;  // 声明一个指向整数的指针变量
int value = 25;
age = &value;  // 将指针指向整数变量 value
2. int *age:

In this way, int and *age are separated, which is also a common way of writing. In this syntax, *age is treated as a pointer variable, pointing to an integer. This way of writing can also clearly indicate that age is a pointer variable pointing to an integer.

int *age;  // 声明一个指向整数的指针变量
int value = 30;
age = &value;  // 将指针指向整数变量 value

In general, these two writing methods are equivalent in most cases, but there may be some subtle differences when multiple variable declarations are involved. For example:

int* age, height;  // 这里 age 是指针,height 是整数变量,可能会产生误导

In this case, to avoid ambiguity, it is recommended to use the following writing:

int *age, height;  // 明确地指示 age 是指针,height 是整数变量

Whichever way you choose, it's important to be consistent and use clear naming and comments in your code to clearly express your intentions.

2. What is the difference between int& age and int &age? The ampersand is close to the data type and close to the variable name:

In C++, int& age and int &age both involve reference declarations, but they have some subtle differences in syntax, involving There will be some impact when it comes to the declaration and use of reference variables.

1. int& age:

In this writing, int& is treated as a whole, which means "integer reference". This syntax emphasizes that variable age is an integer reference. A reference must be initialized immediately upon declaration to refer to an existing integer variable.

int value = 25;
int& age = value;  // 声明一个引用,将其引用到整数变量 value

In this example, age is now a reference to value and any changes to age will actually be directly Reflected onvalue.

2. int &age:

In this way of writing, int and &age are separated, and &age is regarded as a reference variable. an integer. This way of writing also clearly indicates that age is an integer reference.

int value = 30;
int &age = value;  // 声明一个引用,将其引用到整数变量 value

The two writing methods are equivalent in most cases, and both declare an integer reference. However, they may differ slightly when multiple variable declarations are involved, similar to the case with pointers.

int& age, height;  // 这里 age 是引用,height 是整数变量,可能会产生误导

In this case, to avoid ambiguity, it is recommended to use the following writing:

int &age, height;  // 明确地指示 age 是引用,height 是整数变量

Whichever way you choose, it's important to be consistent and use clear naming and comments in your code to clearly express your intentions. Also note that in C++, a reference cannot refer to other variables once it is initialized, so be extra careful when using references.

3. In C++, the usage of & symbol:
1. Use the & symbol in front of a variable to get the address of the variable. For example:
c++int x = 10;
int *p = &x; // 取变量x的地址,将其赋值给指针p

In the above code, the address of variable x is assigned to pointer p.

2. Use the ampersand symbol to indicate a reference in the function parameter list. For example:
c++void func(int &ref) {
    ref = 20; // 修改引用ref的值,相当于修改了传递给函数的实际参数
}
int main() {
    int x = 10;
    func(x); // 传递变量x的引用给函数
    cout << x << endl; // 输出20,因为函数内部修改了引用ref的值,相当于修改了变量x的值
    return 0;
}

In the above code, the function func accepts a reference as a parameter. By modifying the value of the reference, it is equivalent to modifying the actual parameters passed to the function.

In short, the & symbol is used differently when it is close to the data type and when it is close to the variable name, and it needs to be judged according to the specific context.

4. In C++, the usage of * symbol:

In C++, the * sign is also used in two different ways. One is used in front of a pointer variable to indicate that the variable is a pointer, and the other is used in a function parameter list to indicate a reference.

1. Use the * sign in front of a pointer variable to indicate that the variable is a pointer. For example:
c++int x = 10;
int *p = &x; // 取变量x的地址,将其赋值给指针p

In the above code, the variable p is a pointer to type int.

2. Use the * sign in the function parameter list to indicate a reference. For example:
c++void func(int *ptr) {
    *ptr = 20; // 通过指针修改实际参数的值
}
int main() {
    int x = 10;
    func(&x); // 传递变量x的地址给函数
    cout << x << endl; // 输出20,因为函数内部通过指针修改了实际参数的值
    return 0;
}

In the above code, the function func accepts a pointer to type int as a parameter, and modifies the value of the actual parameter through the pointer.

As for the difference between String& name and String& name, it is actually a syntax error. Because in C++ identifiers cannot contain spaces, String& name is invalid syntax. The correct syntax is String& name or std::string& name, indicating that the variable is a reference of type std::string.

Guess you like

Origin blog.csdn.net/qq_33867131/article/details/132585380