Pointer Arithmetic trouble

This is a discussion on Pointer Arithmetic trouble within the C Programming forums, part of the General Programming Boards category; My code is designed to intake a pointer list, which will point to an array, and put all the negative ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    5

    Angry Pointer Arithmetic trouble

    My code is designed to intake a pointer list, which will point to an array, and put all the negative values into a new pointer first, then followed by all the positive values into the new pointer second.
    Code:
    void rearrange(int* list, int* new, int size)
    {
       int i, j=0, temp;
       for(i=0; i<size; i++)
       {
          if(*(list + i) < 0)
          {
              temp= *(list + i); /* I am under the impression that we       
                                           should first "create a house" or an 
                                           address for the pointer new to look at*/
    
    
             (new + j)=&temp; /*This code line fails with the message 
                                          lvalue required as left operand of 
                                          assignment*/
             j++;                   /* Keeps the assignment going along the 
                                          pointer new */
          }
       }
       for(i=0; i<size; i++)
       {
          if(*(list + i) > 0)
          {
             temp = *(list + i);
             new = &temp;
             j++;
          }
       }
    }
    What is my problem?!!!!!
    Thank You

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    put all the negative values into a new pointer first
    Code:
             (new + j)=&temp; /*This code line fails with the message
                                          lvalue required as left operand of
                                          assignment*/
    You are assigning the address of temp to the address of new + j. IOW, you're attempting to change the address pointed to by new + j. I'm pretty sure that's not what you want since temp is locally declared and will disappear once the function returns and you can't change the address of new + j. You need to dereference new + j:

    Code:
    *(new + j) = temp;
    That will assign the value in temp to the address pointed to by new + j. This copies the contents, rather than location of contents.
    Last edited by Cynic; 03-11-2012 at 07:34 PM.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    5
    Thank you very much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer arithmetic
    By danieldcc in forum C Programming
    Replies: 8
    Last Post: 09-30-2011, 12:17 PM
  2. Pointer arithmetic
    By depietro in forum C Programming
    Replies: 13
    Last Post: 03-29-2007, 06:03 AM
  3. Arithmetic on pointer...?
    By Volair in forum C Programming
    Replies: 7
    Last Post: 11-22-2005, 12:58 PM
  4. pointer arithmetic
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-04-2001, 06:45 PM
  5. pointer arithmetic
    By new to C in forum C Programming
    Replies: 3
    Last Post: 09-19-2001, 10:06 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21