字符串数组和Gnome排序

时间:2014-10-10 00:52:16

标签: c arrays string sorting

任务:我正在开发一个程序,该程序应该使用gnome sort对字符串数组进行排序(代码在rosettacode.com上提供)

问题:编译代码后,没有报告错误,但是当我执行程序时,未排序的数组打印出来,我得到"分段错误(核心转储)"错误:

  

____(下午6:46)[ Sergiy @ Ubuntu ]:_____〜> ./a.out

     

未分类:状态0:加利福尼亚州1:俄勒冈州2:华盛顿   状态3:德州zsh:分段错误(核心转储)./a.out

我无法弄清楚问题是什么。绝对错误的排序功能,只是无法弄清楚到底是什么。

源代码

#include<stdio.h>
#include<string.h>


void
gnome_sort (char *a[], int n)
{
  int i = 1, j = 2;
  char temp[20];


//#define swap(i, j) { t = a[i]; a[i] = a[j]; a[j] = t; } 
  while (i < n)
    {
      if (strcmp (a[n - 1], a[n]) > 0)
    {
      //swap(i - 1, i);
      strcpy (temp, a[i]);
      strcpy (a[i], a[i - 1]);
      strcpy (a[i - 1], temp);
      if (--i)
        continue;
    }

      i = j;
      j++;



    }
//# undef swap
}


int
main ()
{

  char *states[] = { "California", "Oregon", "Washington", "Texas" };
  int num_states = 4;

 // int n;

//n=sizeof a / sizeof a[0];

//printf("n is %d ",n);

  int i;
  printf ("\n Unsorted:\n");

  for (i = 0; i < num_states; i++)
    {
      printf ("state %d: %s\n", i, states[i]);
    }

  gnome_sort (states,num_states);

  printf ("\n Sorted: \n");

  for (i = 0; i < num_states; i++)
    {
      printf ("state %d: %s\n", i, states[i]);
    }


  return 0;
}

更新:所以我找到了让程序运行的一种方法。正如用户在这里正确提到的那样,我必须使传递的字符串具有相同的大小,并使用this post here实现。但是当程序进入排序功能时我仍然遇到问题。

更新了代码 1 #include      2 #include

 3  void
 4  gnome_sort (char states[][20], int n)
 5  {

 6    int i = 0, j = 2;
 7    char temp[20];


 8    while (i < n)
 9      {

10        printf ("\n *** Inside while loop *** \n");
11        printf ("\n %d", strcmp (states[i - 1], states[i]));
12        if (strcmp (states[i - 1], states[i]) < 0)
13      {

14        strcpy (temp, states[i]);
15        printf ("\n %s \n", temp);
16        strcpy (states[i], states[i - 1]);
17        strcpy (states[i - 1], temp);
18        if (--i)
19          continue;
20      }

21        i = j;
22        j++;



23      }


24    printf ("\n Sorted in gnome sort: \n");

25    for (i = 0; i < n; i++)
26      {
27        printf ("state %d: %s\n", i, states[i]);
28      }



29  //end of function
30  }


31  int
32  main ()
33  {

34    char states[][20] = { "Washington", "Colorado", "Iowa", "Texas" };
35    int num_states = 4;

36    // int n;

37  //n=sizeof a / sizeof a[0];

38  //printf("n is %d ",n);

39    int i;
40    printf ("\n Unsorted:\n");

41    for (i = 0; i < num_states; i++)
42      {
43        printf ("state %d: %s\n", i, states[i]);
44      }

45    gnome_sort (states,num_states);

46  /*  printf ("\n Sorted: \n");

47    for (i = 0; i < num_states; i++)
48      {
49        printf ("state %d: %s\n", i, states[i]);
50      }*/


51    return 0;
52  }

更新了输出

$ ./a.out

 Unsorted:
state 0: Washington
state 1: Colorado
state 2: Iowa
state 3: Texas

 *** Inside while loop *** 

 -87
 Washington 

 *** Inside while loop *** 

 -87
 Washington 

 *** Inside while loop *** 

 18
 *** Inside while loop *** 

 -6
 Iowa 

 *** Inside while loop *** 

 -73
 Iowa 

 *** Inside while loop *** 

 -17
 Texas 

 *** Inside while loop *** 

 -84
 Texas 

 *** Inside while loop *** 

 -11
 Texas 

 Sorted in gnome sort: 
state 0: Texas
state 1: Iowa
state 2: 
state 3: Colorado
*** stack smashing detected ***: ./a.out terminated
Aborted (core dumped) 

最终解决方案: 堆栈粉碎可能是因为字符串大小不够大,我有点被另一个stackoverflow post暗示。该程序现在可以像我想要的那样工作,但不知道为什么字符串必须长30个字符,因为最大字符串 - 华盛顿 - 长度为10个字符,低于20个字符。

最终代码

1   #include<stdio.h>
     2  #include<string.h>

     3  void
     4  gnome_sort (char states[][30], int n)
     5  {

     6    int i = 1, j = 2;
     7    char temp[30];


     8    while (i < n)
     9      {

    10        printf ("\n *** Inside while loop *** \n");
    11        printf ("\n %d", strcmp (states[i - 1], states[i]));
    12        if (strcmp (states[i - 1], states[i]) > 0)
    13      {

    14        strcpy (temp, states[i]);
    15        printf ("\n %s \n", temp);
    16        strcpy (states[i], states[i - 1]);
    17        strcpy (states[i - 1], temp);
    18        if (--i)
    19          continue;
    20      }

    21        i = j;
    22        j++;



    23      }


    24    printf ("\n Sorted in gnome sort: \n");

    25    for (i = 0; i < n; i++)
    26      {
    27        printf ("state %d: %s\n", i, states[i]);
    28      }



    29  //end of function
    30  }


    31  int
    32  main ()
    33  {

    34    char states[][30] = { "Washington", "Colorado", "Iowa", "Texas" };
    35    int num_states = 4;

    36    // int n;

    37  //n=sizeof a / sizeof a[0];

    38  //printf("n is %d ",n);

    39    int i;
    40    printf ("\n Unsorted:\n");

    41    for (i = 0; i < num_states; i++)
    42      {
    43        printf ("state %d: %s\n", i, states[i]);
    44      }

    45    gnome_sort (states,num_states);

    46  /*  printf ("\n Sorted: \n");

    47    for (i = 0; i < num_states; i++)
    48      {
    49        printf ("state %d: %s\n", i, states[i]);
    50      }*/


    51    return 0;
    52  }

最终输出

$ ./a.out

 Unsorted:
state 0: Washington
state 1: Colorado
state 2: Iowa
state 3: Texas

 *** Inside while loop *** 

 20
 Colorado 

 *** Inside while loop *** 

 14
 Iowa 

 *** Inside while loop *** 

 -6
 *** Inside while loop *** 

 3
 Texas 

 *** Inside while loop *** 

 -11
 Sorted in gnome sort: 
state 0: Colorado
state 1: Iowa
state 2: Texas
state 3: Washington

1 个答案:

答案 0 :(得分:2)

您的排序代码会在a(最初为states)的元素之间复制字符串,但并非所有字符串的大小都相同。因此,如果您将某些内容复制到Texas所在的位置,则该副本将超过Texas结束的位置,从而修改Texas之后存储在该空间中的任何内容。

相关问题