Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unclear explanation of incrementing an array vs a pointer #45

Closed
ohEmily opened this issue Feb 10, 2015 · 3 comments
Closed

Unclear explanation of incrementing an array vs a pointer #45

ohEmily opened this issue Feb 10, 2015 · 3 comments

Comments

@ohEmily
Copy link
Contributor

ohEmily commented Feb 10, 2015

This paragraph is not clear, even though it covers an important distinction:

"In most cases, just calling an array by its variable name without the square brackets will be interpreted as a pointer to the first element of the array. Incrementing a pointer will always move it ahead by the number of bytes making up the type to which it's pointing. In other words, incrementing a pointer makes the pointer move by the size of the type. In this case we're dealing with a pointer to an int, so the pointer will move by 4 bytes to the next int. Unlike a pointer, though, an array is a constant variable (see the code snippet above for how that affects p++ and a++)."

I'll try to find time for fixing this but if anyone else has time, please see if you can word it in a less confusing way!

@cjubb39
Copy link
Member

cjubb39 commented Feb 10, 2015

So I still get email notifs for this so I thought I might add.

I always thought this (perhaps to be reworded for inclusion in recitation notes) excerpt from the C standard was helpful in making the distinction between pointers and arrays:

Except when it is the operand of the sizeof
operator or the unary & operator, or is a character
string literal used to initialize an array of
character type [...] an lvalue that has type “array
of type ” is converted to an expression that
has type “pointer to type ” that points to the
initial member of the array object and is not an
lvalue

Basically just saying that arrays are almost always converted to pointers, yet making it clear that "array of type" is distinct from "pointer to type".

@chmullig
Copy link
Member

Started a revision at #47.

@cjubb39
Copy link
Member

cjubb39 commented Sep 14, 2015

Looks like this was resolved in #47. I'll let someone reopen if that's not the case.

@cjubb39 cjubb39 closed this as completed Sep 14, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants