# * and in C and C++

• 2020-06-03 07:44:42
• OfStack

C + + & And the use of the * 1 is very troubling the difficulty of textbooks blog about these knowledge are divided about its use as a 1, there is no detailed summary, cause I am in this aspect of knowledge structure is chaotic, found 1 in the online English article simply summarizes the two symbols 1 some usage, is a more basic knowledge, I am more concerned about the function pointer, pointer function are not involved, such as the future have time to sort out this part of learning to understand.

C/C + + * and & The use of the

C++ language is a superset of C language. Almost all C programs that can run are C++ programs that can run. Therefore, it is possible to write an C++ program that does not include the C++ feature, although the use of cout and references (see below) would make a better C++ program. C++ and C occasionally differ in their code formats, for example, in C we declare p as a pointer of type int, int *p, whereas in C++ we use int* p (the compiler doesn't care which format you use).

In C/C++, * and & There are many usages, and the exact usage depends on the content of the code.

Use of * in C

(a) Multiplication: x=y*z;

(b) Multiplication assignment: x*=y; Equivalent to x = x * y

(c) Note: /* Here is your note */

(d) declaration of pointer: int *p or int* p; Read: p is a pointer to an integer type.

(e) Composite pointer: int **p; Or int p * *; p is a pointer to a pointer to an integer type. (Same thing, int***p, etc.)

(f) Dereferencing: x=*p assigns the value pointed to by the pointer p to x

& Usage in C

Logic and: if( > 1) & & (b < 0))

(b) bit operation and: x=a & b;

Logic and assignment: x & = y; With x = x & y has the same meaning

C + + & Supplementary usage

One of the variable types in C++ that does not exist refers to a variable (simply a reference), although a similar function can be achieved with Pointers in the C language.

References, Pointers, and addresses are closely related concepts. An address is an address in computer memory, and a pointer is an address variable, so a pointer can "point" to a memory address. Conceptually, a reference variable is essentially another name for a pointer (but cannot be instantiated by the compiler)

It is possible to define a reference within a function like any other variable 1; For example,

``````
void main(void)
{
int i;
int& r = i;
...
} ``````

But this makes no sense, because the use of a reference is the same as the use of a referenced variable.

References are used in function arguments.

``````
void main(void)
{
int i=3;
f(i);
cout << i;
}
void f(int& r)
{
r = 2*r;
} ``````

This program outputs "6" (2*r doubles the variable referenced by r)

In the C language, we can do the same by declaring f() as void f(int *r), where r is a pointer to an integer type, and then calling the argument & i(the address of i) calls the function f(), using a dereference of r within the function f(), but apparently C++ provides a more concise way to pass a value to a function by reference and return a value from a function.