# Thread: test on monday plzzzzz help!

1. ## test on monday plzzzzz help!

Code:
`x[i] = *(x+*(y+1))`
^^^ what does this mean? 2. the full code:

Code:
```#include <stdio.h>

int main()
{
int x[] = {4,3,1,2,0};
int y[] = {0,2,1,4,3};
int i=0;
for (i=0; i<5; i++)
{
x[i] = *(x+*(y+1));
printf("%d\t", x[i]);
}

getch();
}``` 3. Do you understand what *y means if it was in your code?
*y is the value stored at location y; you should be about to figure out that this is the same as the value of y.

Tim S. 4. Originally Posted by stahta01 Do you understand what *y means if it was in your code?
*y is the value stored at location y; you should be about to figure out that this is the same as the value of y.

Tim S.
if *y = y why does this give me 1 instead of 2:

Code:
```#include <stdio.h>

int main()
{
int x = {4,3,1,2,0};
int y = {0,2,1,4,3};
int i=2;
int printthis;
printthis = *(y+i);
printf("%d", printthis);
return 0;
}``` 5. Originally Posted by zer0_r00t if *y = y why does this give me 1 instead of 2:
*y is y. If n is an integral value (not necessarily zero) what do you think *(y + n) is? 6. Originally Posted by grumpy *y is y. If n is an integral value (not necessarily zero) what do you think *(y + n) is?
I'm lost man  7. Indeed. You need to get out your textbook or course notes, and read up on pointers.

This is a homework exercise. People in this thread have given you all the information they can within the constraint of not doing your homework for you. If they did that, you would learn exactly nothing - and the purpose of homework is for YOU to learn, not to regurgitate an answer given by someone else, that you don't understand.

You might want to read this site's homework policy (here). 8. Originally Posted by grumpy Indeed. You need to get out your textbook or course notes, and read up on pointers.

This is a homework exercise. People in this thread have given you all the information they can within the constraint of not doing your homework for you. If they did that, you would learn exactly nothing - and the purpose of homework is for YOU to learn, not to regurgitate an answer given by someone else, that you don't understand.

You might want to read this site's homework policy (here).
ok at least tell me what does *(y+i) mean if y=0 and i=2
Clues plzzzz 9. ok at least tell me what does *(y+i) mean if y=0 and i=2
O_o

The value of `y' is not relevant in determining the value of `*(y + i)'.

Soma 10. Originally Posted by zer0_r00t ok at least tell me what does *(y+i) mean if y=0 and i=2
Clues plzzzz
Nothing. You asking the wrong question (in fact, your question is meaningless - which is another sign you need to actually read your textbook or course notes).

Big hint: Understanding what y+i means is an ESSENTIAL first step to understanding what *(y+i) is. 11. Look under "Pointer arithmetic" to figure out what y+i means.

* is being used as the Indirection operator in *(y+i) .

Tim S. 12. Originally Posted by zer0_r00t ok at least tell me what does *(y+i) mean if y=0 and i=2
Clues plzzzz
Try doing this :

Code:
`printf("array[%d] = %d, at location %p", i, *(y+i), (void*) (y+i));`
That plus some reading on pointers : Chapter 1 13. I agree with the other posters; the knowledge you seem to be missing here is how arrays work and how the indirection operator references element values. The best place I personally know of to read about this information is in chapter 10 (Arrays and Pointers) of "C Primer Plus" by Stephen Prata (the book I learned with). There are many places to get equivalent information; I am only intimately familiar with that one.

To answer your original question, for you to properly understand what's going on in that expression, first break down the constituent parts as they relate to each other in the entire program. This should always be the first thing you do when you're trying to figure out code that doesn't make sense upon first glance.

What is x?
In your program, x is an array of integers, with five elements (0-4).

What is i?
In your program, i is an integer variable being used as a loop counter, and also as an array element number variable. Incidentally, you don't need to initialize i when you declare it because you're doing that in the first part of the for loop argument, before it is actually referenced.

What is y?
In your program, y is an array of integers, with five elements (0-4).

What is x[i] = *(x+*(y+1));?
In your program, you are assigning the value of an expression to the i element of the x array.

What is *(x+*(y+1))?
Have you learned about indirection (dereferencing) and pointer arithmetic yet? Both of those subjects are of high importance in understanding the expression. Remember in nested expressions, it's correct to work outward from the innermost parenthetic expression.
In your program, once you consider the previous information regarding the constituent parts, the expression is:
*(y+1) which is saying, "take the value of the second element in the y array"
(x+ which is saying, "use that value to move to a certain element in the x array"
* which is saying, "once you've arrived at the correct element in the x array, give me its value"

That value of the x array's referenced element is the value that your for loop prints five times.

You may be curious as to how the program can successfully use i to count and simultaneously reference an x array element with the same variable...

This is because during the process, the loop counter does its job of counting to 4, but it also changes the value of all the x array elements to the one value printed. You can include simple printf() statements before and after the loop to verify this.

As a matter of fact, to better understand the behavior of different parts of a program, printf() statements are extremely useful. You can always comment them out or delete them once you're satisfied you understand what's going on. Popular pages Recent additions 