2014-02-23 50 views
0
#include <stdio.h> 
#include <string.h> 

typedef struct word 
{ 
char *p; 
int index; 

}Word; 

typedef struct wordarray 
{ 
    Word **array; 
    int count; 

}arr; 

arr* createarray(int count) 
{ 
    arr *temp=(arr*)malloc(sizeof(arr)); 
    temp->array=(Word**)malloc(sizeof(Word*)*count); 
    temp->count=count; 
    return temp; 

} 
int my_partition(char p[],int start,int end) 
{ 
    int left=start; 
    start++; 
    while(start<=end) 
    { 
     while(start<=end && p[start] < p[left]) 
      start++; 
     while(start<=end && p[end]> p[left]) 
      end--; 

      if(start<end) 
      p[start]=(p[start]^p [end]^ (p[end]=p[start])); 
    } 
    p[end]=(p[left]^p [end]^ (p[left]=p[end])); 
    return end; 

} 
void my_sort(char *p, int start, int end) 
{ 
    //printf("\n%d %d\n",start,end); 
    if(start<=end) 
    { 
     int pivot=my_partition(p,start,end); 
     my_sort(p,start,pivot-1); 
     my_sort(p,pivot+1,end); 
    } 


} 
void addWord(arr* temp, char p[][10]) 
{ 
    int i=0; 


    for(i=0;i<4;i++) 
    { 
     temp->array[i]=(Word*)malloc(sizeof(Word)); 
     temp->array[i]->p=(char*)malloc(strlen(p[i])+1); 
     temp->array[i]->index=i; 
     strcpy(temp->array[i]->p,p[i]); 
     temp->array[i]->p[3]='\0'; 
     // printf("\n %c \n",temp->array[i]->p[0]); 
     char *q=(temp->array[i]->p); 
     //printf("\n %s \n",q); 
      my_sort(q,0,2); 
    } 

} 
int partition(arr *temp, int start, int end) 
{ 
    int left=start; 
    start++; 
while(start<=end) 
{ 
    while(start<=end && (strcmp(temp->array[start]->p,temp->array[left]->p) < 0)) 
     start++; 
    while(start<=end && (strcmp(temp->array[end]->p,temp->array[left]->p)> 0)) 
     end--; 

     if(start<end) 
     { 
      Word *temp1=temp->array[start]; 
      temp->array[start]=temp->array[end]; 
      temp->array[end]=temp1; 
      //temp->array[start] 
     } 
} 
      Word *temp1=temp->array[left]; 
      temp->array[left]=temp->array[end]; 
      temp->array[end]=temp1; 

return end; 


} 
void qsort(arr *temp, int start, int end) 
{ 
    if(start <end) 
    { 
     int pivot=partition(temp,start,end); 
     qsort(temp,start,pivot-1); 
     qsort(temp,pivot+1,end); 
    } 
} 
void print(arr *temp) 
{ 
    int i=0; 
    for(i=0;i<(temp->count);i++) 
     printf("\n %s \n",temp->array[i]->p); 

} 
main() 
{ 

arr *temp=createarray(4); 
char p[][10]={"act","cat","pat","cap"}; 

addWord(temp,p); 
qsort(temp,0,3); 
print(temp); 
} 

在上面的程序中,my_sort會對字符串進行排序。現在,當我將p [i]而不是q傳遞給my_sort函數時,它工作正常。但是當我傳遞q時,它會進入無限循環。我可以在上面的語句中打印q的值。這裏有什麼問題?未能將字符指針傳遞到函數

+0

0 ==結果是什麼的定義'arr'? –

+0

@ Code-Guru新增 – debonair

+0

嘗試在您完成{(temp-> array [i] - > p =(char *)malloc(strlen(p [i])+ 1);}後將最後一個字符清零 –

回答

0

"act","cat"my_sort更改爲"act","act"

在分區

while(start<=end) 
{ 
    while(start<=end && (strcmp(temp->array[start]->p,temp->array[left]->p) < 0)) 
     start++; 
    while(start<=end && (strcmp(temp->array[end]->p,temp->array[left]->p)> 0)) 
     end--; 

     if(start<end) 
     { 
      Word *temp1=temp->array[start]; 
      temp->array[start]=temp->array[end]; 
      temp->array[end]=temp1; 
     //temp->array[start] 
     } 
} 

滯留在這個while循環時的strcmp(temp->array[start]->p,temp->array[left]->p)