无法将字符指针传递给函数

时间:2014-02-23 03:06:23

标签: c pointers

 #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的值。这有什么不对?

1 个答案:

答案 0 :(得分:0)

"act","cat"已由"act","act"更改为my_sort

在分区

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]
       }
}

当0 == strcmp(temp->array[start]->p,temp->array[left]->p)

的结果时,被困在这个while循环中