^^^ what does this mean?Code:x[i] = *(x+*(y+1))
^^^ what does this mean?Code:x[i] = *(x+*(y+1))
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(); }
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[0].
Tim S.
"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson
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).
O_ook at least tell me what does *(y+i) mean if y[0]=0 and i=2
You have already received two answers to that question.
The value of `y[0]' is not relevant in determining the value of `*(y + i)'.
Soma
“Salem Was Wrong!” -- Pedant Necromancer
“Four isn't random!” -- Gibbering Mouther
Look under "Pointer arithmetic" to figure out what y+i means.
* is being used as the Indirection operator in *(y+i) .
Tim S.
Last edited by stahta01; 05-31-2014 at 08:40 PM.
"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson
Try doing this :
That plus some reading on pointers : Chapter 1Code:printf("array[%d] = %d, at location %p", i, *(y+i), (void*) (y+i));
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.
V8 Interceptor: KDE 5.25.5 on Manjaro Linux 22.0.0 "Sikaris"
Steering wheel: gcc 12.2.0 in Kate
Supercharger: NASM 2.15.05
Engine: AMD Ryzen 7 1700
Dashboard: NVIDIA GeForce GTX 1060 6GB
Rusty old trailer for hauling 3% of my Steam catalog: Windows 7 Pro 64bit SP1
3 Antique Ford Model T automobiles for vintage LAN gaming: Windows XP SP3
Sturdy buckboard for DOS LAN gaming: DOSBox 0.74-3 on Windows XP SP3