Thread: test on monday plzzzzz help!

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    12

    Question test on monday plzzzzz help!

    Code:
    x[i] = *(x+*(y+1))
    ^^^ what does this mean?

  2. #2
    Registered User
    Join Date
    Apr 2014
    Posts
    12
    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. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    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

  4. #4
    Registered User
    Join Date
    Apr 2014
    Posts
    12
    Quote Originally Posted by stahta01 View Post
    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.
    if *y = y[0] why does this give me 1 instead of 2:

    Code:
    #include <stdio.h>
    
    
    int main()
    {
        int x[5] = {4,3,1,2,0};
        int y[5] = {0,2,1,4,3};
        int i=2;
        int printthis;
        printthis = *(y+i);
        printf("%d", printthis);
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by zer0_r00t View Post
    if *y = y[0] why does this give me 1 instead of 2:
    *y is y[0]. If n is an integral value (not necessarily zero) what do you think *(y + n) is?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Apr 2014
    Posts
    12
    Quote Originally Posted by grumpy View Post
    *y is y[0]. If n is an integral value (not necessarily zero) what do you think *(y + n) is?
    I'm lost man

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Apr 2014
    Posts
    12
    Quote Originally Posted by grumpy View Post
    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]=0 and i=2
    Clues plzzzz

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    ok at least tell me what does *(y+i) mean if y[0]=0 and i=2
    O_o

    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

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by zer0_r00t View Post
    ok at least tell me what does *(y+i) mean if y[0]=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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    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

  12. #12
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by zer0_r00t View Post
    ok at least tell me what does *(y+i) mean if y[0]=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. #13
    Codus Conjectus spongefreddie's Avatar
    Join Date
    Sep 2010
    Location
    USA
    Posts
    86
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. don't be like this guy / monday lols
    By m37h0d in forum C++ Programming
    Replies: 1
    Last Post: 10-17-2011, 10:56 AM
  2. Asteroid almost hit the earth on Monday
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 03-06-2009, 11:37 PM
  3. can anyone help me...plzzzzz
    By emyD in forum C Programming
    Replies: 5
    Last Post: 10-02-2007, 03:06 AM
  4. Plzzzzz Help Me >>> Big Problem
    By AHMED KHALAF in forum C Programming
    Replies: 2
    Last Post: 12-09-2004, 07:31 AM
  5. Last Monday's Game
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 10-30-2003, 06:55 PM