开发者

C pointers: how to say this code

开发者 https://www.devze.com 2023-03-27 09:52 出处:网络
I found this page with the following explanation of pointers: http://www.woyouxian.net/c/c0501.html int x = 1, y = 2, z[10];

I found this page with the following explanation of pointers:

http://www.woyouxian.net/c/c0501.html

int x = 1, y = 2, z[10];
int *ip;          /* ip is a pointer to int */
ip = &x;          /* ip now points to x */
y = *ip;          /* y is now 1 */
*ip = 0;          /* x is now 0 */
ip = &z[0];       /* ip now points to z[0] */

However, the lines "y is now 1" and "x is now 0" describe the results, not the code. How would one "say" those lines, to describe the code (as the other lines do)?

In other words, the line "y is now 1" does not implicitly have the literal "1" on the line, so the description is 开发者_开发技巧describing the result of the code and not the code itself. I'd like a description of the code itself.


C is not a spoken language, so opinions here will vary quite considerably.

I'd say and comment it like this:

int x = 1, y = 2, z[10];
int *ip;          /* declares a pointer-to-int called "ip" */
ip = &x;          /* makes "ip" point to "x" */
y = *ip;          /* sets "y" to the value of what "ip" points to, i.e. 1 */
*ip = 0;          /* sets the value of what "ip" points to to 0 */
ip = &z[0];       /* makes "ip" point to the first element in the array "z" */

Or, to stay closer to your original comments:

int x = 1, y = 2, z[10];
int *ip;          /* ip is a pointer to int */
ip = &x;          /* ip now points to x */
y = *ip;          /* y's value is now equal to the value of the object that "ip" points to */
*ip = 0;          /* the value of the object that "ip" points is now 0 */
ip = &z[0];       /* ip now points to z[0] */

Disclaimer

I wouldn't actually write comments like this into code. Code comments are for explaining rationale; code syntax should be self-explanatory.

What I meant was that I'd annotate the code like that, if I had to for a purpose like yours.


The line y = *ip means "assuming 'ip' is a pointer (i.e. a memory address), go to its memory address, retrieve the value, and store it in variable y". Since the previous line set ip to the address of x, whose value was 1, the value of y after executing that line is also 1.

The line *ip = 0 means "go to the address stored in ip (i.e. the address of x's value), and set the value there to 0.


int x = 1; /* create a integer variable x and set it to 1 */
int y = 2; /* create a integer variable y and set it to 2 */
int z[10]; /* create a list z of 10 integers  */
int *ip; /* create a pointer to an intger ip */
ip = &x; /* let ip point to the variable x */
y = *ip; /* let y be the value that ip points at */
*ip = 0; /* set the value that ip points at to 0 */
ip = &z[0]; /* let ip point at the first value in the list z */


y = *ip : Set the value of the variable y to what ip points to.

*ip = 0 : Set the value of the pointer ip to 0. Since ip is a pointer,*ip accesses what ip is pointing to.

Side note (handy terminology): In both of the above, * is used to dereference the pointer ip.


Is this what you were looking for?

int x = 1, y = 2, z[10];
int *ip;          /* declare ip as a pointer to an int */
ip = &x;          /* assign the address of x to ip (i.e. the value of ip is now the address of x) */
y = *ip;          /* assign y to the value of the memory at which ip points */
*ip = 0;          /* assign the value of the memory at which ip points to 0 */
ip = &z[0];       /* assign ip the address of the first element of the z array */


I generally pronounce "x = 1" as "x becomes 1". Others might say "x is 1" or "x equals 1".

In your code, I would say "y becomes the value pointed to by ip" and "the value pointed to by ip becomes 0".


"the value of the address ip points to is copied into y"

"the value 0 is copied into the address ip points to"


y = *ip;    /* y is now the value stored in location pointed by ip */
*ip = 0;    /* value stored in location pointed by ip is now 0 */
0

精彩评论

暂无评论...
验证码 换一张
取 消