繁体   English   中英

如何在程序中将字符串数组传递给函数?

[英]How to pass an array of strings to a function in this program?

我是C的初学者,我们在课堂上有一个工作分配,以获取给定的字符串列表,将它们放入字符串数组中,然后将其传递给用户定义的排序函数,该函数按字母顺序打印。 每当我运行我的代码时,它都不会给出任何编译器错误,但它也会在运行时立即崩溃。 调试给了我一个分段错误,但它没有给我造成错误的特定行。 我正在通过Dev C ++中包含的gcc编译器运行代码。 这是我的代码。 任何帮助,将不胜感激。 我认为我的问题是尝试将字符串数组传递给该函数,但是我无法找到我能理解的主题的任何答案。

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

void sort(char *[]);

int main()
{
    char *states[4] = {0};
    states[0] = "Florida";
    states[1] = "Oregon";
    states[2] = "California";
    states[3] = "Georgia";

    sort(states);

    return 0;
}

void sort(char *ptr[])
{
    int i, j;
    char temp[20];
    for ( i = 1; i <= 4; i++ )
    {
        for ( j = 1; j <= 4; j++ )
            {
                if (strcmp(ptr[j-1], ptr[j]) > 0)
                {
                    strcpy(temp, ptr[j-1]);
                    strcpy(ptr[j-1], ptr[j]);
                    strcpy(ptr[j], temp);
                }
            }
    }

    int x;
    for ( x = 0; x < 4; x++ )
    {
        printf("%s", ptr[x]);
        printf("\n");
    }
}

我看到的问题:

  1. 您在for循环中使用了错误的索引。

    代替:

     for ( i = 1; i <= 4; i++ ) { for ( j = 1; j <= 4; j++ ) 

采用:

    for ( i = 0; i < 4; i++ )   // Keep the values in the range 0 - 3.
    {
        for ( j = 0; j < 4; j++ )
  1. 您正在修改只读内存。

    使用时:

     states[0] = "Florida"; 

    states[0]具有包含字符串"Florida"的只读地址的值。 如果您修改了该地址上的值(正在执行sort ,那么您将输入未定义的行为范围。

您可以通过切换指针而不是复制值来解决问题。

    // Use char* for temp instead of an array
    char* temp;
    if (strcmp(ptr[j-1], ptr[j]) > 0)
    {
        temp = ptr[j-1];
        ptr[j-1] = ptr[j];
        ptr[j] = temp;
    }

附录,对OP的评论

以下版本的sort适用于我:

void sort(char *ptr[])
{
   int i, j;
   char* temp;
   for ( i = 0; i < 4; i++ )
   {
      // NOTE:
      // This is different from your version.
      // This might fix your problem.
      for ( j = i+1; j < 4; j++ )
      {
         if (strcmp(ptr[j-1], ptr[j]) > 0)
         {
            temp = ptr[j-1];
            ptr[j-1] = ptr[j];
            ptr[j] = temp;
         }
      }
   }

   for ( i = 0; i < 4; i++ )
   {
      printf("%s", ptr[i]);
      printf("\n");
   }
}

崩溃的原因是j <= 4 另一个问题是您要交换指向字符串而不是字符的指针。

void sort(char *ptr[])
{
    int i, j;
    char *temp;
    for (i = 0; i < 4; i++) // sticking to array boundary convention
    {
        for ( j = 1; j < 4; j++ )
        {
            if (strcmp(ptr[j-1], ptr[j]) > 0)
            {
                // swap pointers 
                tmp = ptr[j];
                ptr[j] = ptr[j-1]; 
                ptr[j-1] = ptr[j];
            }
        }
    }

    for (i = 0; i < 4; i++)
        printf("%s\n", ptr[i]);
}

C的标准库已经带有对任意数据数组进行排序的功能。 它称为qsort

qsort可以使用您的第四个参数(比较函数)根据您提供的任何条件对数组进行排序。 在这种情况下,我们有一个自然的比较函数,我们希望将它与qsort期望的结合起来使用。

因此,下面的mycmpstr适当地转换了传入的指针,并使用strcmp返回比较字符串的结果。 我们知道qsort将使用常量指针指向常量char指针的方式调用我们的比较函数,并且我们会适当地“投射”。

请记住,您正在对指向字符数组的指针进行排序。 这意味着,您不需要移动字符串本身,只需移动指针。 您可以使用strcmp根据指向的字符串的顺序来确定哪个指针应该位于哪个指针之前。

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

int mycmpstr(const void *p, const void *q) {
    const char *const *x = p;
    const char *const *y = q;
    return strcmp(*x, *y);
}

int main(void)
{
    int i;
    const char *states[] = {
        "Florida", "Oregon", "California", "Georgia"
    };

    qsort(
        states,
        4,
        sizeof(char *),
        mycmpstr
    );

    for (i = 0; i < 4; i += 1) {
        puts(states[i]);
    }

    return 0;
}

输出:

$ clang -Wall -O1 sortex.c -o sortex
$ ./sortex
California
Florida
Georgia
Oregon

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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