0
#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] = -1112

Process 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 :)

3
  • 6
    That this compiles amazes me, since clang reports an appropriate error : "main.c:25:14: Incompatible pointer types passing '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 do char* 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). Commented May 26, 2016 at 6:02
  • 1
    str_swap(&char_array[0],&char_array[2]); shouldn't compile, because &char_array[0] and &char_array[2] are not char**s. Commented May 26, 2016 at 6:03
  • Swapping 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. Commented May 26, 2016 at 6:16

3 Answers 3

2

Yes, you're missing something here. char [4][100] is an array of arrays, not an array of pointers. You're function expects the addresses of pointers, not addresses of arrays (arrays are not pointers).

Changing your array to be an array of const char* (pointer to constant char data), and your function properly swap pointers to pointers of that genre will fix your problem.

#include <stdio.h>
#include <stdlib.h>

char const *char_array[4] = {"-11111","-1111","-1110","-1112"};

void str_swap(char const ** str1,char const ** str2);

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 const ** str1, char const ** str2)
{
    char const *temp = *str1;
    *str1 = *str2;
    *str2 = temp;
}

Output

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] = -1110
char_array[1] = -1111
char_array[2] = -11111
char_array[3] = -1112

How your original code runs is a mystery, as no C compiler I use will allow what you're doing to even compile, much less run.

Sign up to request clarification or add additional context in comments.

Comments

2

The code you provide doesn't compile as the type of &char_array[0] is not char** which the str_swap function expects to get. Basically you could probably achive what you want by making these changes:

#include <string.h>

char temp[100];
...
void str_swap(char* a,char* b);
...
str_swap(char_array[0],char_array[2]);//you don't need to use the & operator here
...
void str_swap(char* str1,char* str2) 
{
   strncpy(temp,str1,100);//copy the first to temp
   strncpy(str1,str2,100);//copy the second to the first
   strncpy(str2,temp,100);
}

4 Comments

This will take a lot of time for large strings as you are copying strings from one address to another instead of just swapping the addresses. @LPs answer is a more efficient swapping technique
@Nishant You are right. i didn't want to change the OP's original deceleration for the strings but using the array of pointer for the string will make it much more efficient. I was also surprised the code compiled in the first place :)
@bkVnet what's the type of &char_array[0]?
@BlackMamba in this case since deceleration is char char_array[4][100], it is a pointer to an array of 100 characters, i.e char (*)[100]
2

To compile your code you have to change your matrix into an array of pointers to literals c-string, so

char char_array[4][100]= {"-11111","-1111","-1110","-1112"};

must be

char *char_array[4]= {"-11111","-1111","-1110","-1112"};

Your code should be

#include <stdio.h>
#include <stdlib.h>

char *char_array[]= {"-11111","-1111","-1110","-1112"};

#define ARRAY_SIZE  sizeof(char_array)/sizeof(char_array[0])

void str_swap(char** a,char** b);

int main() {
    size_t j=0;

    printf("BEFORE str_swap(&char_array[0],&char_array[2]);\n");

    for (j=0; j<ARRAY_SIZE; j++) {
        printf("char_array[%zu] = %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<ARRAY_SIZE; j++) {
        printf("char_array[%zu] = %s\n",j,char_array[j]);
    }

    return 0;
}

void str_swap(char** str1, char** str2) {
    char* temp;
    temp = *str1;
    *str1 = *str2;
    *str2 = temp;
}

Output will be

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] = -1110
char_array[1] = -1111
char_array[2] = -11111
char_array[3] = -1112

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.