Yahoo Clever wird am 4. Mai 2021 (Eastern Time, Zeitzone US-Ostküste) eingestellt. Ab dem 20. April 2021 (Eastern Time) ist die Website von Yahoo Clever nur noch im reinen Lesemodus verfügbar. Andere Yahoo Produkte oder Dienste oder Ihr Yahoo Account sind von diesen Änderungen nicht betroffen. Auf dieser Hilfeseite finden Sie weitere Informationen zur Einstellung von Yahoo Clever und dazu, wie Sie Ihre Daten herunterladen.

Why does C++ pointer syntax tie the pointer to the variable and not the type?

For example, why is the standard pointer declaration this:

int *a;

char *x,*y,*z;

Instead of this:

int* a;

char* x,y,z;

To me this makes little sense - when you declare a variable, e.g. "float p,q,r;" this says "I want to create float variable(s) - those variables are p, q, and r;" So why is it not valid to say "char* x,y,z:" means "I want to create some char pointer variable(s) - those variables are x, y, and z."

How would you even translate "char *x,*y,*z" into english? "I want to create some char variable(s) - those variables are pointer x, pointer y, pointer z?" It's not like pointers are some special type of data - they're just like any other class, struct, or data type, so why have special syntax to declare them?

Update:

You can even do

typedef int* intptr;

intptr x,y,z; // all int*

int* p,q,r; // only p is an int*

How does this make sense!?

3 Antworten

Relevanz
  • Cubbi
    Lv 7
    vor 1 Jahrzehnt
    Beste Antwort

    This, like the rest of pointer black magic, is one of the C language features that C++ supports, so to answer your question, you have to go into the history of C.

    Kernighan, a co-creator of C, decided that if the type of p is pointer-to-int, then it would look better if the type of "*p" was always int. Because int is what you'd get if you place "*p" in an expression, then it must also be what you get if you place "*p" in a declaration. Thus, to his mind, the line "int *p, q, *r, s" reads as "*p of type int, q of type int, *r of type int, s of type int"

    Of course you're right to say that pointer types are types like any other and that's why today most C code style guidelines advise against declaring more than one variable per statement (or at least against mixing pointers and non-pointers in one line). In C++ both uninitialized objects and raw pointers are very rarely used to begin with, so the issue almost never arises, but if it does, same guidelines are usually followed.

  • Anonym
    vor 5 Jahren

    1.To answer your PS, the & is the address operator which creates an "address literal" if you will. So yes that is valid. (Also you are not "declaring" anything. You are simply telling the function to expect an address) The variable x, unless dereferenced, will always be manipulated as an address. so the operation of adding 1 will bump the pointer to the next address location, which is system dependent based on the size of an integer. 2. f(p,p) is rather unintelligent, but there point was that you are passing the variable p by val and by ref. I.E. f(&p, p); Technically I wouldn't ever say f(p,p); 3. Two variables themselves can never have the same address. Two pointers can point to the same address however. 4.X is not an integer. X is an integer pointer. 5. Good luck.

  • Matt
    Lv 5
    vor 1 Jahrzehnt

    Well, putting the * before the variable allows you to declare a pointer and non-pointer in the same line.

    But, you don't even need a space, you could just put int*a;

    Although intuitivly it would make sense for it to be int* p, q, r as opposed to int*p,*q,*r.

    I guess it's just a little quirk to live with :P

    Or, maybe, you should think of

    int*n, as I want to make a pointer to an integer. Think of it as, I want to make an integer, and store the address of that variable in this variable instead.

Haben Sie noch Fragen? Jetzt beantworten lassen.