【问题标题】:Pass char pointer array to function将 char 指针数组传递给函数
【发布时间】:2016-03-27 22:50:50
【问题描述】:

我正在尝试将一个已初始化的 char 指针数组传递给一个函数。我似乎无法弄清楚为什么该函数只会打印出数组中每个元素的数字。

有谁知道如何打印传入的指针数组中的每个字符串元素?

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

void sort(char *);

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

   sort(*states);

   return 0;
}

void sort(char *states)
{
   int x;

   for (x = 0; x < 4; x++) {
      printf("\nState: %d\n", states[x]); //only this will compile
      //printf("\nState: %s\n", states[x]); //need to print this.
   }

}

【问题讨论】:

  • 您将指向Florida 的指针传递给函数,然后打印出与该字符串的前四个字母相对应的数字。这不是你想要的吗?
  • sort(states);void sort(char *states[])
  • 好的,我明白了。所以我不接受指针数组。

标签: c arrays function pointers char


【解决方案1】:

如果您希望打印数组的内容,您的 sort 函数必须接受指针数组。

void sort (char *states[], size_t num_states) {
    int x;

    for (x = 0; x < num_states; ++x) {
        printf("\nState: %s\n", states[x]); /* Note %s instead of %d */
    }
}

而且,您必须将数组传递给函数。

sort(states, 4);

【讨论】:

  • sort 函数必须接受指向指针的指针。最终,char *states[] 等价于 char **states 作为函数参数,因此 char *states[] 不是指针数组。
  • @hacks:你是对的。来自调用者的参数是一个数组的名称,sort 函数必须有一个允许传递该参数的原型。数组名称衰减为与其第一个元素的地址相等的指针值,因此sort 需要接受指向char * 的指针。
  • 所以 main 中的 char 数组指针总是最初指向该数组中的第一个元素,对吗?
  • @user3495404; main 中没有字符数组指针。 states 是一个指向 char 的指针数组。在函数调用sort(states, 4);中,states转换为char **(指向数组states的第一个元素的指针,即指向state[0]的指针)。
【解决方案2】:

您需要将指向 char 的指针数组解析为 sort(而不仅仅是指向 char 的指针)。

正如 jhx 所指出的,您还需要传递数组的大小。您可以使用sizeof,以免硬编码4。初始化时也省略了数组大小。

void sort( char *states[], int arr_size )
{
    int x;

    for (x = 0; x < arr_size; x++) 
    {
        printf( "\nState: %s\n", states[x] );
    }
}

int main()
{
    char *states[] = {"Florida", "Oregon", "California", "Georgia"};     // array of pointers to char

    sort( states, sizeof( states ) / sizeof( char * ) );

    return 0;
}

【讨论】:

  • 你能解释一下 sizeof(states) / sizeof(char *) 是做什么的吗?
  • states 是一个指向 char 的指针数组。 char * 是指向 char 的指针。所以这个除法指定了states指针数组中元素的数量(即char指针的数量)。
【解决方案3】:

你需要将char指针数组传递给函数:

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

    void sort(char *args[], int n);

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

       sort(states, 4);

       return 0;
    }

    void sort(char *states[], const int N)
    {
       int x;

       for (x = 0; x < N; x++) {
          printf("\nState: %s\n", states[x]); 
       }

    }

【讨论】:

    【解决方案4】:

    您仅获得数值的原因是仅传递了指向数组states 的字符串states[0] 的第一个元素的指针,即您正在传递&amp;states[0][0]。所以,声明

    printf("\nState: %d\n", states[x]);  
    

    只会打印字符串"Florida"的前4个字符的数值。

    您需要将指针传递给数组states 的第一个元素,即&amp;states[0]
    这可以通过将函数 sort 的声明符更改为

    void sort(char **, size_t size); // You need to pass the size of array too. 
    

    并将其称为

    sort(states, sizeof(states)/sizeof(char *));
    

    【讨论】:

      【解决方案5】:

      您发送的是states,这是一个array of pointers。所以你需要将该数组的基地址发送给函数

      sort(*states);
      

      然后只在一个指针数组中接收它

      void sort(char* states[])
      {
         int x;
      
         for (x = 0; x < 4; x++) {
            printf("\nState: %s\n", states[x]);
         }
      
      }
      

      硬编码大小也不是一个好主意,所以最好在函数调用中添加另一个参数size

      sort(states, sizeof(states)/sizeof(char*));
      

      然后用它来迭代数组

      void sort(char* states[], int size)
      {
         int x;
      
         for (x = 0; x < size; x++) {
            printf("\nState: %s\n", states[x]);
         }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-07
        • 1970-01-01
        • 2021-12-02
        • 2012-07-28
        • 1970-01-01
        • 1970-01-01
        • 2011-05-22
        相关资源
        最近更新 更多