#include <stdio.h>
#include <stdlib.h>
char* temp=" ";
char char_array[4][100]= {"-11111","-1111","-1110","-1112"};
void str_swap(char** a,char** b);
int main(int argc,char* argv[]) {
int j=0,n=4;
printf("BEFORE str_swap(&char_array[0],&char_array[2]);\n");
for (j=0; j<n; j++) {
printf("char_array[%d] = %s\n",j,char_array[j]);
}
str_swap(&char_array[0],&char_array[2]);
printf("AFTER str_swap(&char_array[0],&char_array[2]);\n");
for (j=0; j<n; j++) {
printf("char_array[%d] = %s\n",j,char_array[j]);
}
return 0;
}
void str_swap(char** str1,char** str2) {
temp = *str1;
*str1 = *str2;
*str2 = temp;
}
Hi all:I want to do char* array element swapping in C,and above is my code .However I got nothing changed as output belown shown.Am I missing something here?Thanks for your help in advance.
BEFORE str_swap(&char_array[0],&char_array[2]);
char_array[0] = -11111
char_array[1] = -1111
char_array[2] = -1110
char_array[3] = -1112
AFTER str_swap(&char_array[0],&char_array[2]);
char_array[0] = -11111 //should be -1110
char_array[1] = -1111
char_array[2] = -1110 //should be -11111
char_array[3] = -1112Process returned 0 (0x0) execution time : 0.359 s
Press any key to continue.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 1002
char *char_array[MAXLEN];
void str_swap(char const** str1,char const** str2);
int my_compare_func(int,int);
void bubble_sort(int,int(*compare_func)(int,int));
int main(int argc,char* argv[]) {
int n=0,i=0,j=0;
while(scanf("%d",&n)==1) {
i=n;
while(i--) {
scanf("%s",&char_array[i]);
}
printf("BEFORE BULLE SORT\n");
for (j=0; j<n; j++) {
printf("j = %d\n",j);
printf("char_array[%d] = %s\n",j,&char_array[j]);
//printf("char_array[%d] = %s\n",j,char_array[j]);
}
bubble_sort(n,my_compare_func);
printf("AFTER BULLE SORT\n");
for (j=0; j<n; j++) {
printf("j = %d\n",j);
printf("char_array[%d] = %s\n",j,&char_array[j]);
//printf("char_array[%d] = %s\n",j,char_array[j]);
}
system("PAUSE");
}
return 0;
}
int my_compare_func(int x,int y) {
int i=0,res = 0,len_strx=0,len_stry=0;
len_strx = strlen(char_array[x]);
len_stry = strlen(char_array[y]);
if(char_array[x][0]=='-') {
if(char_array[y][0]=='-') {
if(len_strx>len_stry) {
res = -1;
}else if(len_strx<len_stry) {
res = 1;
}else if(len_strx==len_stry) {
i=1;
while((char_array[x][i]==char_array[y][i])&&(char_array[x][i]!='\0')&&(char_array[y][i]!='\0')) {
++i;
}
if(i==len_strx) {
res = 0;
}else if(char_array[x][i]<char_array[y][i]){
res = 1;
}else if(char_array[x][i]>char_array[y][i]) {
res = -1;
}
}
}else if((char_array[y][0]>='0'&&char_array[y][0]<='9')||char_array[y][0]=='+'){
res = -1;
}
}else if((char_array[x][0]>='0'&&char_array[x][0]<='9')||char_array[x][0]=='+') {
if((char_array[y][0]>='0'&&char_array[y][0]<='9')||char_array[y][0]=='+'){
if(len_strx>len_stry) {
res = 1;
}else if(len_strx<len_stry) {
res = -1;
}else if(len_strx==len_stry) {
i=0;
while(char_array[x][i]==char_array[y][i]&&char_array[x][i]!='\0'&&char_array[y][i]!='\0') {
++i;
}
if(i==len_strx) {
res = 0;
}else if(char_array[x][i]<char_array[y][i]){
res = -1;
}else if(char_array[x][i]>char_array[y][i]){
res = 1;
}
}
}else if(char_array[y][0]=='-') {
res = 1;
}
}
return res;
}
void str_swap(char const** str1,char const** str2) {
char const* temp = *str1;
*str1 = *str2;
*str2 = temp ;
}
void bubble_sort(int n,int(*compare_func)(int x,int y)) {
int i=0,j=0,flag=1;
for(i=n-1; (i>0)&&(flag==1); --i) {
flag=0;
for(j=0; j<i; ++j) {
if(compare_func(j,j+1)>0) {
str_swap(&char_array[j],&char_array[j+1]);
flag = 1;
}
}
if(!flag) {
break;
}
}
}
Thank you for all the answers.Your response is very fast.In fact I am trying to solve the basic online judge problem.Check it here:(in mandarin) http://zerojudge.tw/ShowProblem?problemid=a528
Basically it's a sorting problem with string storing big number(both positive and negative).Sorry for posting such huge amount lines of code.fast.My new problem is that while I have my code running to the line:
//printf("char_array[%d] = %s\n",j,&char_array[j]);
printf("char_array[%d] = %s\n",j,char_array[j]);
then the console application crashed!I tried some approach like the commented line(that is,set the %s part in printf() function to be &char_array[j]).It works fine but contradicts my knowledge about arguments > of printf().Can anyone explain this for me?Thanks :)
'char (*)[100]' to parameter of type 'char **'", a message that should be heeded with extreme interest. You have an array of arrays, not an array of pointers, so your statement "I want to dochar*array element swapping" is an impossibility. That function isn't right for what you're trying to accomplish (or that array isn't right for that function, take your pick; either spell doom).str_swap(&char_array[0],&char_array[2]);shouldn't compile, because&char_array[0]and&char_array[2]are notchar**s.char[]is not the same as swapping pointers. If you want to swap pointers, your array needs to contain pointers, not arrays (and your swap function is close to correct, btw). If you want to swap entire arrays, see bkVnet's answer below.