Thread: swap columns in two dimensional array with pointers

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    47

    swap columns in two dimensional array with pointers

    Hi ,trying to swap columns in two dimensional array with pointers
    fswap-first column to swap
    sswap-second column to swap
    What am I doing wrong
    Thank you

    Code:
    void swapColumns(double a[][6],int row,int col,int fswap,int sswap)
     {
        int i;
        double *px,temp;
        px=a;
       for (i=0;i<row ;i++) 
       {   
         temp=*(px+fswap);
         *(px row*col+fswap) = *(px row*col+sswa)p;
         *(px row*col+sswap)=temp; 
       }  
      }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    How are you going to "walk" down the column, without your loop iterator being involved?

    Every time you loop, you change i, and nothing else. But you loop logic doesn't use i at all.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    47
    I changed it but still dont work
    Code:
    void swapColumns(double a[][6],int row,int col,int fswap,int sswap)
     {
        int i;
        double *px,*temp;
        px=a;
       for (i=0;i<row ;i++) 
       {   
          temp=*(px i*col+fswap);
         *(px i*col+fswap) = *(px i*col+sswap);
         *(px i*col+sswap)=temp; 
       }  
      }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    What exactly are you trying to swap?

    > px=a;
    In particular, why are you using a bone-headed approach like this, when you already have the ability to index the array using [r][c] notation.
    It would certainly simplify your code.

    Code:
    temp = a[i][from];
    a[i][from] = a[i][to];
    a[i][to] = temp;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    47
    I am trying to swap two columns in two dimensional array(lets say second and forth).And I have to use pointers and pointer arithmetic

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > temp=*(px i*col+fswap);
    And C doesn't have a 'space' operator.

    Maybe write it the obvious way first, just to make sure it ACTUALLY works, then try to mung it to how your "prof" would like it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    47
    I did write it the obvious way ,but when I go with pointers is giving mi errors
    Code:
     void swapColumns(double a[][6],int row,int col,int fswap,int sswap)
     {
        int i;
        double temp;
       for (i=0;i<row ;i++) 
       {   
         temp=a[i][fswap];
         a[i][fswap]=a[i][sswap];
         a[i][sswap]=temp; 
       }  
      }
    POinters -sintax error before i
    Code:
    void swapColumns(double a[][6],int row,int col,int fswap,int sswap)
     {
        int i;
        double *px,*temp;
        px=a;
       for (i=0;i<row ;i++) 
       {   
          temp=*(px i*col+fswap);
         *(px i*col+fswap) = *(px i*col+sswap);
         *(px i*col+sswap)=temp; 
       }  
      }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    As Salem noted, there is no 'space' operator, so syntax like px i is wrong (except in some special cases).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    47
    Is there a way to fix that

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Just leave it in index notation.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Nov 2010
    Posts
    47
    Thanks ,But I still don't understand
    Could you give me an example

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Perhaps you meant to add the calculated offset?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Swapping Rows and Columns in a 2D array
    By xxshankar in forum C Programming
    Replies: 2
    Last Post: 03-11-2010, 03:40 PM
  2. Multi dimensional array
    By $l4xklynx in forum C Programming
    Replies: 7
    Last Post: 01-03-2009, 03:56 AM
  3. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM