0
$\begingroup$

For example, when I look at some code like

int x = 1;
int * y = &x;

I can read it like

x equals 1
y equals the address of x

but how would I "read" something like

*y = 1;

My best guess is

The value at address y equals 1

But I'm wondering if there's a better way to think about it.

$\endgroup$
7
  • $\begingroup$ How you read code is up to you. There could be common conventions (which I am unaware of), but you don't have to follow them. Personally I read it "star y equals one". $\endgroup$ – Yuval Filmus Jan 19 '18 at 14:28
  • $\begingroup$ This interesting questions seems somewhat more appropriate to stackoverflow, or even to English Language & Usage. $\endgroup$ – Yuval Filmus Jan 19 '18 at 14:28
  • $\begingroup$ @YuvalFilmus I wouldn't ask this kind of technical question on ELU. $\endgroup$ – David Richerby Jan 19 '18 at 14:32
  • $\begingroup$ I don't think this has much to do with computer science, since it depends completely on the syntax of the language in question. It's also not quite clear what you mean. You read "cat" as "cat" not "a four-legged animal that chases mice" so why do you call it "reading" when you say "the value at address y" instead of "star y"? $\endgroup$ – David Richerby Jan 19 '18 at 14:34
  • $\begingroup$ read such expressions from right to left.. $\endgroup$ – user3629249 Jan 19 '18 at 16:55
2
$\begingroup$

I always read *y =1 as: assign the value $1$ to the memory location pointed by y.

Why?

y is a pointer type variable, so it has an address as a value. It another words, it points to this address. When we want to, indirectly, manipulate the memory area pointed by y we use *y. Hence *y=1 tells that the value $1$ should be stored in the memory location pointed by y.

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.