Arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dru
    New Member
    • Sep 2006
    • 29

    Arrays

    Hi I have a question from ab ook im trying to understand but can't get it. If someone can shoot me in the right direction i would appreciate it.

    The question is:
    Assume that ip has been declared to be a pointer to int and that result has been declared to be an array of 100 elements. Assume further that ip has been initialized to point to an element in the first half of the array.

    Write an expression whose value is the element in the array after the element that ip points to.

    My code is:
    Code:
    *ip[++&ip]
    Please help thanks
    DRU
  • arne
    Recognized Expert Contributor
    • Oct 2006
    • 315

    #2
    Originally posted by dru
    Hi I have a question from ab ook im trying to understand but can't get it. If someone can shoot me in the right direction i would appreciate it.

    The question is:
    Assume that ip has been declared to be a pointer to int and that result has been declared to be an array of 100 elements. Assume further that ip has been initialized to point to an element in the first half of the array.

    Write an expression whose value is the element in the array after the element that ip points to.

    My code is:
    Code:
    *ip[++&ip]
    Please help thanks
    DRU

    I thought the array is called 'result' ...?
    You may try
    Code:
    *ip++;
    This should give the next value in the array after the value 'ip' points to.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Originally posted by arne
      Code:
      *ip++;
      That doesn't return the element in the array after the element that ip points to.

      Because you have use post increment they value of the expression is the value of the element in the array that ip points to. Then once evaluated it increments ip to point to the next element.

      Try any of these
      Code:
      *++ip;
      
      *(ip+1);
      
      ip[1];

      Comment

      • arne
        Recognized Expert Contributor
        • Oct 2006
        • 315

        #4
        Originally posted by Banfa
        That doesn't return the element in the array after the element that ip points to.

        Because you have use post increment they value of the expression is the value of the element in the array that ip points to. Then once evaluated it increments ip to point to the next element.

        Try any of these
        Code:
        *++ip;
        
        *(ip+1);
        
        ip[1];
        Oops, yes, you are right ...

        Comment

        Working...