Why do we use "type * var" instead of "type & var" when defining a pointer?

I am relatively new to C ++ (about a year of experience, on and off). I am wondering what led to the solution type * nameas a syntax for defining pointers. It seems to me that the syntax should be type & name, since the character is &used everywhere in the code to refer to the address of the memory variable. So, to use a traditional example of pointers int:

int a = 1;
int * b = &a;

will become

int a = 1;
int & b = &a

I am sure that there are some reasons for this that I simply don’t see, and I would like to hear some materials from C ++ veterans.

Thanks, -S

+5
source share
6 answers

++ C. "" C" (Dennis Ritchie) C * , .

[ ] : , , [ *] . , , . ,

int i, *pi, **ppi;

, , . , i, * pi ** ppi int .

:

int *(*foo)[4][];

, *(*foo)[4][0] int, ( [] , *), : foo - 4 ints.

++ C. , , ++ .

int & b = a;

, int. , , , , , .

int x = 5, y = 10;
int& r = x;

int sum = r + y; // you do not need to say '*r' automatically dereferenced.

r = y; // WRONG, 'r' can only have one thing pointing at during its life, only at its infancy ;)
+7

, C:

, : index , , . , . ,

int i, *pi, **ppi;

, integer, . i, * pi ** ppi int . ,

int f(), *f(), (*f)();

, integer, , , ;

int *api[10], (*pai)[10];

. , , .

, type * var , () .

, "NB", "B", int pointer[] int, int array[10], int s.

+4

, , . , .

- , , , , .

, , ++ , . - , C , .

, ++, , . & someVariable, , , someVariable. , !

, Apple Objective-C , int *someIntPointer int* someIntPointer

IMHO, - C, , .

someIntPointer , . , , , :

int* a, b;  // b is a straight int, was that our intention?

int  *a, *b;  // old-style C declaring two pointers

int* a;
int* b;  // b is another pointer to an int

, , , , , .

+3

C-, ++. , , - .

'&' . , .

"*" --. , .

, . .

+1

int* b "b int", int *b: "* b - int". & anti- *: * b - int. *b - &*b b.

+1

, " K & R".

. , .

K & Rs are those who decided that the C syntax for declaring pointers was.

It is not int and x; instead of int * x; because the language that defines the guys who created it is K & R.

-2
source

All Articles