title: How to do char* array element swapping in C?
body: <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  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(&amp;char_array[0],&amp;char_array[2]);\n");
  
      for (j=0; j&lt;n; j++) {
          printf("char_array[%d] = %s\n",j,char_array[j]);
      }
  
  
      str_swap(&amp;char_array[0],&amp;char_array[2]);
  
  
      printf("AFTER str_swap(&amp;char_array[0],&amp;char_array[2]);\n");
  
      for (j=0; j&lt;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;
  }
  </code></pre>
  
  <p>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.</p>
  
  <blockquote>
    <blockquote>
      <p>BEFORE str_swap(&amp;char_array[0],&amp;char_array[2]);<br/> char_array[0] = -11111<br/> char_array[1] = -1111<br/> char_array[2] = -1110<br/> char_array[3] = -1112<br/> AFTER
      str_swap(&amp;char_array[0],&amp;char_array[2]);<br/> char_array[0] = -11111
      //should be -1110<br/> char_array[1] = -1111<br/> char_array[2] =
      -1110  //should be -11111<br/> char_array[3] = -1112<br/></p>
    </blockquote>
    
    <p>Process returned 0 (0x0)   execution time : 0.359 s<br/> Press any key
    to continue.<br/></p>
  </blockquote>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #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",&amp;n)==1) {
          i=n;
          while(i--) {
              scanf("%s",&amp;char_array[i]);
          }
          printf("BEFORE BULLE SORT\n");
          for (j=0; j&lt;n; j++) {
              printf("j = %d\n",j);
              printf("char_array[%d] = %s\n",j,&amp;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&lt;n; j++) {
              printf("j = %d\n",j);
              printf("char_array[%d] = %s\n",j,&amp;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&gt;len_stry) {
                  res = -1;
              }else if(len_strx&lt;len_stry) {
                  res = 1;
              }else if(len_strx==len_stry) {
                  i=1;
                  while((char_array[x][i]==char_array[y][i])&amp;&amp;(char_array[x][i]!='\0')&amp;&amp;(char_array[y][i]!='\0')) {
                      ++i;
                  }
                  if(i==len_strx) {
                      res = 0;
                  }else if(char_array[x][i]&lt;char_array[y][i]){
                      res = 1;
                  }else if(char_array[x][i]&gt;char_array[y][i]) {
                      res = -1;
                  }
              }         
          }else if((char_array[y][0]&gt;='0'&amp;&amp;char_array[y][0]&lt;='9')||char_array[y][0]=='+'){
              res = -1;        
          }
      }else if((char_array[x][0]&gt;='0'&amp;&amp;char_array[x][0]&lt;='9')||char_array[x][0]=='+') {
          if((char_array[y][0]&gt;='0'&amp;&amp;char_array[y][0]&lt;='9')||char_array[y][0]=='+'){
              if(len_strx&gt;len_stry) {
                  res = 1;
              }else if(len_strx&lt;len_stry) {
                  res = -1;
              }else if(len_strx==len_stry) {
                  i=0;
                  while(char_array[x][i]==char_array[y][i]&amp;&amp;char_array[x][i]!='\0'&amp;&amp;char_array[y][i]!='\0') {
                      ++i;
                  }
                  if(i==len_strx) {
                      res = 0;
                  }else if(char_array[x][i]&lt;char_array[y][i]){
                      res = -1;
                  }else if(char_array[x][i]&gt;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&gt;0)&amp;&amp;(flag==1); --i) {
          flag=0;
          for(j=0; j&lt;i; ++j) {
              if(compare_func(j,j+1)&gt;0) {
                  str_swap(&amp;char_array[j],&amp;char_array[j+1]);
                  flag = 1;
              }
           }  
           if(!flag) {
               break;
           }
      }
  }
  </code></pre>
  
  <blockquote>
    <p>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)
    <a href="http://zerojudge.tw/ShowProblem?problemid=a528" rel="nofollow">http://zerojudge.tw/ShowProblem?problemid=a528</a><br/>
    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:<br/>
      //printf("char_array[%d] = %s\n",j,&amp;char_array[j]);<br/>
      printf("char_array[%d] = %s\n",j,char_array[j]);<br/>
    then the console application crashed!I tried some approach like the 
    commented line(that is,set the %s part in printf() function to be 
    &amp;char_array[j]).It works fine but contradicts my knowledge about arguments > of printf().Can anyone explain this for me?Thanks :)</p>
  </blockquote>