简体   繁体   中英

Swap between array of strings only by pointers in C

I need to plot a function that receives an array of pointers (to strings) and sort the pointers by length of string. The shortest string will be in the first place, and so on. I tried the following code but it doesn't work:

void Q5(){
  char str[MAXL][MAXC];
  int i;
  char** p;
  printf("Please enter %d Strings max length = %d\n",MAXL,MAXC);
  for (i = 0; i < MAXL; i++){
    scanf("%s", str[i]);
  }
  p = &str;
  sort_String(p,MAXL);
  printf("\n");
  for (i = 0; i < MAXL; i++){
    printf("%s", str[i]);
    printf("\n");
  }
}

void sort_String(char* str,int size){
char* tempP=*str;
char* i,*j;
for (i=str; i < str+size;i++){
    for (j=i+1; j < str+size; j++){
        if (strlen(i) > strlen(j)){
            tempP = j;
            j = i;
            i = tempP;
        }
    }
}

To achieve what you said you should dynamically malloc the necessary part then you can use this code. But you can't change a arrays positions(locations) That is not allowed.

// L-> number of strings
// C-> character needed

char **str=malloc(sizeof(char*)*L);
for(i=0;i<MAXL;i++)
  str[i]=malloc(sizeof(char)*C);

Then you can simply apply the bubble sort that you have written. You need to pass the char** in sorting function.

Iterating over the pointer variables in sort function is not needed. If you allocate dynamically as I have mentioned then you can code it this way:-

void sort_String(char** str,int size)
{
char* tempP;
int i,j;
for (i=0; i < size;i++){
    for (j=i+1; j < size; j++){
        if (strlen(str[i]) > strlen(str[j])){
            tempP = str[j];
            str[j ]= str[i];
            str[i] = tempP;
        }
    }
}
}

There are few issues though like you have not given a proper code there is error like Q5()'s } is missing. Give MCV example.


Note: I assumed that you are using ac compiler. That's why I have avoided the casting. In C, you don't need to cast the return value of malloc. The pointer to void returned by malloc is automatically converted to the correct type. However, if you want your code to compile with a C++ compiler, a cast is needed.

An array of pointers is not the same string as a 2D array, even if they are used the same to get a value. To be able to swap strings, you must have an array of pointers. Here is a commented quickfix to your code, because you were not that far from it:

void Q5(){
  char str[MAXL][MAXC]; // Ok a 2D array is nice to read the string
  int i;
  char* p[MAXL]; // the array of MAXL pointers
  printf("Please enter %d Strings max length = %d\n",MAXL,MAXC);
  for (i = 0; i < MAXL; i++){
    scanf("%s", str[i]);       // the string is read
    p[i] = str[i];             // and the array of pointers is initialized
  }
  sort_String(p,MAXL);
  printf("\n");
  for (i = 0; i < MAXL; i++){
    printf("%s", p[i]);       // only p has been sorted, str is unchanged
    printf("\n");
  }
}

void sort_String(char** str,int size){ // takes an array of pointers as first parameter
char* tempP;
int i, j;
for (i=0; i < size;i++){   // ok for a bubble sort
    for (j=i+1; j < size; j++){
        if (strlen(str[i]) > strlen(str[j])){  // compare length of pointed strings
            tempP = str[j];             // swap
            str[j] = str[i];
            str[i] = tempP;
        }
    }
}

I assumed that MAXL and MAXC were both constant values ( #define )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM