ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.