-2

My question is a simple one (I hope) about c syntax regarding pointer declaration. I am fully aware of how to declare a pointer, how its used and what the effects are, like as follows.

int *val_ptr;
int val =99;

val_ptr = &val;

However, what confuses me is why when we declare a pointer in C do we use the * Indirection (value of) operator? and not the & address of operator. If we are declaring a pointer would it not make sense to do so with &, because we are declaring an address right? Example:

int & val_ptr;
int val =99;

val_ptr = &val;

I know it's incorrect but to my mind that would seem more intuitive. What is it I'm missing in my conception of the * operator. I have not yet found a text book that gives me an explanation of why, they just show how. I know how, I would like to know why.

Thanks for reading.

Andrew S
  • 2,847
  • 3
  • 33
  • 50
  • 3
    "Why?" has a single correct answer: because the standard says so. – Jon Jan 26 '14 at 21:52
  • 2
    Wouldn't this more be a question for [programmers.stackexchange.com/](http://programmers.stackexchange.com/)? – AntonH Jan 26 '14 at 21:53
  • @jon I know that standard says so, but why it seems counter intuitive. – Andrew S Jan 26 '14 at 21:55
  • 2
    This question appears to be off-topic because it belongs on programmers.stackexchange.com – old_timer Jan 26 '14 at 21:55
  • 3
    @AndrewS You seem to be asking us to burrow into the mind of Ritchie et al. We cannot give you a definitive answer as to why, beyond "the creators deem it so" – Sinkingpoint Jan 26 '14 at 21:56
  • you can use int ptr[] if you dont like *ptr. – old_timer Jan 26 '14 at 21:56
  • 1
    @dwelch: Eeeeewwwwwwwwwww. – Jon Jan 26 '14 at 21:57
  • The answer is: Why not? – Ivan Aksamentov - Drop Jan 26 '14 at 21:57
  • 1
    Why all the hate, its just a question about implementation. You folks are so unforgiving. – Andrew S Jan 26 '14 at 21:57
  • @Quirliom: That sounded like a line out of some scifi story :) – Jon Jan 26 '14 at 21:57
  • 2
    As others have pointed out, this question is probably not answerable by anybody other than the designers of the language. – dandan78 Jan 26 '14 at 21:59
  • 3
    @AndrewS: IMO the question cannot be answered to StackOverflow's standards, but that does not make it a bad question in itself -- just a bad fit for this site (programmers.stackexchange would be better). You didn't deserve the downvotes, but this is the internet. People are going to do whatever. – Jon Jan 26 '14 at 22:01
  • 1
    John, thanks for that. I just thought someone might be able to present a notional model that explains why. It would seem the baying angry mob don't like questions that question. – Andrew S Jan 26 '14 at 22:10
  • Ritchie chosen `*` and not `&` or `^`, because he knew, that many years later Stroustroup will need `&` for references and Microsoft will need `^` for their handles =) – Ivan Aksamentov - Drop Jan 26 '14 at 22:21

4 Answers4

4

The pointer declaration syntax tries to mimic pointer usage. When you have

int *ptr;

then *ptr is of the type int.

Wojtek Surowka
  • 20,535
  • 4
  • 44
  • 51
2

Declaration mimics use. If you have a pointer to an integer and you want to access the pointed-to value, you dereference the pointer with the * operator, like so:

x = *p;

The type of the expression *p is int; therefore, it follows that the declaration of p should be

int *p;
John Bode
  • 119,563
  • 19
  • 122
  • 198
1

When you write:

int *mypointer;

you can think, "I declare that (*mypointer) is an int" (i.e, "my pointer will reference an int"), like this:

int (*mypointer);

Look that now,

int (&mypointer);

has no sense. What are you saying? the "address of my pointer value is an int" (that's true, but it's not your point).

lando
  • 269
  • 1
  • 9
0

You can't use & operator to declare pointer in C. Standard doesn't allow this. When you declare pointer in C like:

int *val_ptr;  

then * in the declaration is not the dereference (indirection) operator. But when this * comes in a statement then it performs indirection.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • I know, I mention that in my OP. But why is that the standard, it seems counter intuitive. I'm sure there is a conceptual reason for it and I just wanted to see if anyone knew. – Andrew S Jan 26 '14 at 21:59
  • Could you explain why `1` is read as `One` not `Apple`? Is there a reason behind this I am not aware about ? – haccks Jan 26 '14 at 22:01
  • That is such a closed minded attitude. Newton questioned why the apple fell from the tree. Whist others said 'because everything falls downwards, silly'. – Andrew S Jan 26 '14 at 22:04
  • I know, this is an open mind attitude. If C standard defines `*` in such a way that when this token comes after a data type then it will declare a pointer then what is the problem? – haccks Jan 26 '14 at 22:07
  • 1
    @AndrewS I think that the difference between the Newton laws and C is that latter was completely designed by human beings. That means that they could do whatever they wanted and not be guided by any logic of laws of nature. So asking why was that done so can be meaningless. Other example of human creations: why math operators of addition is looks like "+" and subtraction like "-"? Why not vice- versa? Are there any logic behind this? – Ivan Aksamentov - Drop Jan 26 '14 at 22:12
  • To truly understand a language is to know why. I'm sure that choice was made deliberately and not by chance. – Andrew S Jan 26 '14 at 22:13
  • @Drop, I'm sure Richie was guided by complete rational logic when he chose * to be used as both value of and for declaring pointer. My guess is he didn't just randomly make it up as he went along. – Andrew S Jan 26 '14 at 22:16
  • 1
    @AndrewS You would be surprised how much of computing history was sheer coincidence, or just inherited from some predecessor. The answer here though, is (as Wojtek has answered here) that it's because in there's a symmetry in `int *j;` the expression `*j` yields an int, and that design is somewhat recorded [here](http://cm.bell-labs.com/who/dmr/chist.html). Do note that your question is phrased in a very subjective way, noone knows or care why using `*` confuses you. – nos Jan 26 '14 at 22:39
  • 1
    This one addressed my question the most and it kind of clarify some of my misconception that when used to declare a variable it's not actually an indirection operator, but just signifies that the type is a pointer. A actually asked the question on retro computing forum, after the negative response gotten here, and got a full clear short and concise answer by someone how really understood the language implementation. – Andrew S Jan 27 '14 at 03:19