Date: Fri, 27 Apr 2001 19:30:49 -0600 From: Duke Normandin <01031149@3web.net> To: Freebsd Questions <freebsd-questions@freebsd.org> Subject: OT - C Pointers Message-ID: <20010427193048.A70243@mandy.rockingd.calgary.ab.ca>
next in thread | raw e-mail | index | archive | help
Hi guys... Little OT if I might -- please! Ted Jensen, in his "A Tutorial on Pointers and Arrays in C" states, "But, back to using our new variable ptr. Suppose now that we want to store in ptr the "address" of our integer variable k. To do this we use the unary '&' operator and write: ptr = &k; The '&' operator's job is to: * retrieve the lvalue (address) of k, even though k is on the right hand side of the assignment operator '=', and * copy the address to the contents of our pointer ptr. Now, ptr is said to "point to" k." Ruurd Pels, in his "A Brief Tutorial on Pointers, Lvalues, & Rvalues" states: [some text snipped for brevity] " Definitions: rvalue the attribute of a variable that holds the address where that particular variable is stored. lvalue the attribute of a variable that holds the value of the variable. int v = 3; int* p; p = &v; <---- What the &-operator does is a modification of what happens at the left side of the assignment. Basically it tells the compiler not to use 'v'-s rvalue to obtain the address where the lvalue of 'v' is stored, but it tells it to use the rvalue of 'v' as the right hand side of the assignment. It then proceeds as normal, assigning the value obtained to the lvalue of p and storing that at the address contained in the rvalue of p. What we have now is a p with an rvalue that is equal to the address where p is stored, and an lvalue that is equal to the address where 'v' is stored. Bingo! We initialized a pointer. Conclusion: - The meaning of the symbol '&' in the context of a variable is "take-the-address-instead-of-the-value" " I'm confused!! In his "Definitions:" above, did Pels get rvalue and lvalue switched around? I ask because if you re-read what each states about the meaning of '&', they don't seem to jibe. Or, am I missing the obvious -- if there's such a thing with pointers? ;) Here's my take. Given: int i, k, *j; i = 3 k = i; <-- Case1 j = &i; <-- Case2 In Case1, lvalue of k -> the address of k rvalue of k -> the rvalue of i (via the lvalue of i) In Case2, lvalue of j -> the address of j rvalue of j -> the lvalue (address) of i So I lean towards Jensen's explanation, and the '&' means: DON'T use the lvalue of i to get the rvalue of i, to then do the assignment as normally would happen DO use the lvalue of i *only* for the assignment. So who's correct - Jensen and I; or Pels? And while we're *there* *j -> always refers to the rvalue of i TIA.... -- -duke Calgary, Alberta, Canada To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20010427193048.A70243>