LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-22-2007, 04:50 AM   #1
andystanfordjason
Member
 
Registered: Oct 2006
Location: england
Distribution: fedora 6
Posts: 38

Rep: Reputation: 15
c pointers


hello, i am writing an implementation of gaussian elimination and have set up a matirx as follows: int matrix[n][n]. part of the algorithm requires swapping rows and was wondering, in order to be efficient, if the pointers to the rows could be swapped? is this possible? if so any cluse as to how to do it would be greatly appreciated. thanks a lot
 
Old 04-22-2007, 08:46 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
If you do not ever want to restore the orginal matrix, it's fine.
Code:
int matrix[24][24]={0};
...........
void swap(int row1, int row2, int matrix[][24])
{
   int tmp24={0};
   memcpy(tmp, matrix[row1], 24 * sizeof(int));
   memcpy(matrix[row1], matrix[row2], 24 * sizeof(int));
   memcpy(matrix[row2], tmp, sizeof(int) *24);
}
You can also swap individual row pointers if you create a int **ptrs, which is probably faster, except that a block swap is fairly efficient on most platforms.
Then you have to refernece the array elements by indirection from then on.
 
Old 04-22-2007, 09:46 AM   #3
andystanfordjason
Member
 
Registered: Oct 2006
Location: england
Distribution: fedora 6
Posts: 38

Original Poster
Rep: Reputation: 15
thanks thats perfect
 
Old 04-22-2007, 04:23 PM   #4
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 48
as jim mcnamara said, you can also do the swap with **char; here's compilable code that does that (although I've used char *line[ROW] instead of char **line to avoid an extra malloc() ). I was also busy using chars instead of ints, because I wanted to be able to easily printf() a given line.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ROW 6
#define COL 10

char *line[ROW];

int main(void){
        char ch;
        char *placeholder;

        /* for each row in *line[], allocate space for 11 characters,
           set the first 10 characters to the current value of 'ch',
           put a null character at the end. */
        for( ch='a'; ch <= 'f'; ch++ ){
                line[ ch - 'a' ] = malloc( (COL + 1) * sizeof(char) );
                memset(line[ ch - 'a' ], ch, COL);
                line[ ch - 'a' ][COL] = '\0';
                printf("%s\n", line[ ch - 'a' ]);
        }

        /* Swap first and last lines*/
        placeholder = line['f' - 'a'];
        line['f' - 'a'] = line['a' - 'a'];
        line['a' - 'a'] = placeholder;

        /* print results */
        printf("=== Switcheroo ===\n");
        for( ch='a'; ch <= 'f'; ch++ ){
                printf("%s\n", line[ ch - 'a' ]);
        }

        return 0;
}

Last edited by bartonski; 05-13-2007 at 10:07 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers in C studentlb Programming 6 11-27-2006 02:53 PM
pointers hubabuba Programming 1 10-26-2005 07:47 PM
Looking for some pointers... p3ngu!n Linux - Newbie 17 10-30-2003 06:46 AM
pointers in C again h/w Programming 9 10-29-2003 11:46 PM
need help with pointers qanopus Programming 8 02-03-2003 05:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration