C 将字符指针数组传递给函数

C 将字符指针数组传递给函数,c,arrays,function,pointers,char,C,Arrays,Function,Pointers,Char,我试图将初始化的字符指针数组传递给函数。我似乎不明白为什么函数只打印出数组中每个元素的数字 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 */ } } 有人知道如何打印传入指针数组中的每个字符串

我试图将初始化的字符指针数组传递给函数。我似乎不明白为什么函数只打印出数组中每个元素的数字

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 */
    }
}
有人知道如何打印传入指针数组中的每个字符串元素吗

#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.
   }

}
#包括
#包括
#包括
#包括
无效排序(字符*);
int main()
{
char*州[4]={“佛罗里达州”、“俄勒冈州”、“加利福尼亚州”、“乔治亚州”};
排序(*状态);
返回0;
}
无效排序(字符*状态)
{
int x;
对于(x=0;x<4;x++){
printf(“\n状态:%d\n”,状态为[x]);//只有这个才会编译
//printf(“\n状态:%s\n”,状态[x]);//需要打印此文件。
}
}

如果要打印数组的内容,则
排序
函数必须接受指针数组

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 */
    }
}

您需要将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]); 
       }

    }
#包括
#包括
#包括
#包括
无效排序(字符*args[],整数n);
int main()
{
char*州[4]={“佛罗里达州”、“俄勒冈州”、“加利福尼亚州”、“乔治亚州”};
排序(状态,4);
返回0;
}
无效排序(字符*状态[],常量int N)
{
int x;
对于(x=0;x
您正在发送
状态
,这是一个
指针数组
。因此,您需要将该数组的基址发送给函数

sort(*states);
然后只在指针数组中接收它

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

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

}
然后用它来迭代数组

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

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

}
void排序(字符*状态[],整数大小)
{
int x;
对于(x=0;x
之所以只获取数值,是因为只传递了指向数组
状态的字符串
状态[0]
的第一个元素的指针,即传递
状态[0][0]
。那么,声明呢

printf("\nState: %d\n", states[x]);  
将只打印字符串
“Florida”
的第一个
4
字符的数值

您需要将指针传递到数组的第一个元素
状态
,即
状态[0]

这可以通过将函数
sort
的声明符更改为

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

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

您需要解析指向char to
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;
}
void排序(字符*状态[],整数arr\u大小)
{
int x;
对于(x=0;x
将指向佛罗里达州的指针传递给函数,然后打印出该字符串前四个字母对应的数字。这不是你想要的吗?
sort(states)
无效排序(char*states[])
好的,我明白了。所以我不接受指针数组。你能解释一下sizeof(states)/sizeof(char*)在做什么吗?
states
是指向char的指针数组
char*
是指向char的指针。因此,该除法指定
状态
指针数组中的元素数(即字符指针数)。
排序
函数必须接受指向指针的指针。归根结底,
char*states[]
作为函数参数等同于
char**states
,因此
char*states[]
不是指针数组。@haccks:你是对的。调用方的参数是数组的名称,
sort
函数必须具有允许传递该参数的原型。数组名衰减为等于其第一个元素地址的指针值,因此
sort
需要接受指向
char*
的指针。因此main中的char数组指针总是在最初指向该数组中的第一个元素,对吗?@user3495404;
main
中没有字符数组指针
states
是指向
char
的指针数组。在函数调用中
排序(状态4)
状态
转换为
字符**
(指向数组
状态
的第一个元素的指针,即指向
状态[0]
)。
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;
}