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

Code tracing 6 CS 31 (Spring '16) #2

Open
geeick opened this issue Dec 6, 2024 · 0 comments
Open

Code tracing 6 CS 31 (Spring '16) #2

geeick opened this issue Dec 6, 2024 · 0 comments

Comments

@geeick
Copy link

geeick commented Dec 6, 2024

The solution for the code doesn't match the question. It has an extra variable "d".
Question:
int main() {
int a[] = {1, 2, 3, 4, 5};
int *b;
int c;
b = &
(a + 1);
c = b - 1;
c[0] = 6;
b += 2;
c = b + 1;
cout << *a << *b << *c;
}

Solution:
int main() {
int a[] = {1, 2, 3, 4, 5};
int *b;
int c, d; // c is a int ptr, d is an int
b = &
(a + 1); // 1. b points to a[1]
c = b - 1; // 2. c points to a[0]
*b = *c; // 3. a[1] = a0
d = *c; // 4. d = a0
c[0] = 6; // 5. a[0] = 6 (notice d doesn’t change)
b += 2; //6. b points to a[1+2] or a[3]
cout << *a << *b << *c << d;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant