【发布时间】: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