打印数组指针的C函数?
我试图制作一个c程序,打印出数组的指针,这就是我所尝试的打印数组指针的C函数?,c,C,我试图制作一个c程序,打印出数组的指针,这就是我所尝试的 #include <stdio.h> #include <string.h> void printArr(int index,char *arr); char *str[] = {"heyyo","help"}; int main() { //printf(*(str+1)); --Works printArr(1,str); // --No output return 0; }
#include <stdio.h>
#include <string.h>
void printArr(int index,char *arr);
char *str[] = {"heyyo","help"};
int main()
{
//printf(*(str+1)); --Works
printArr(1,str); // --No output
return 0;
}
void printArr(int index,char *arr){
printf(*(arr+index));
}
#包括
#包括
void printArr(整数索引,字符*arr);
char*str[]={“heyyo”,“help”};
int main()
{
//printf(*(str+1));--作品
printArr(1,str);//--无输出
返回0;
}
void printArr(整数索引,字符*arr){
printf(*(arr+索引));
}
函数不起作用,因此没有输出
void printArr(int index,char **arr){
printf("%s\n",*(arr+index));
}
玩你自己
自己玩代码中存在类型不匹配
str
是指向char数组的指针,而函数采用指向char的指针
test.c:11:16: warning: passing argument 2 of ‘printArr’ from incompatible pointer type
printArr(1,str); // --No output
^
test.c:4:6: note: expected ‘char *’ but argument is of type ‘char **’
void printArr(int index,char *arr);
代码中存在类型不匹配
str
是指向char数组的指针,而函数采用指向char的指针
test.c:11:16: warning: passing argument 2 of ‘printArr’ from incompatible pointer type
printArr(1,str); // --No output
^
test.c:4:6: note: expected ‘char *’ but argument is of type ‘char **’
void printArr(int index,char *arr);
你如何编译你的程序?如果是GCC或Clang,则将
-pedantic errors-Wall-Wextra
添加到编译器调用中。这将是非常有启发性的。只要“帮助”它的工作,如果我做printf(*(str+1));请注意,每次编写*(数组+索引)
,您都会通过使用一些糖和编写array[index]
得到完全相同的结果,这更加清晰。@同样清晰地展开。您如何编译程序?如果是GCC或Clang,则将-pedantic errors-Wall-Wextra
添加到编译器调用中。这将是非常有启发性的。只要“帮助”它的工作,如果我做printf(*(str+1));请注意,每次编写*(数组+索引)
,您都会通过使用一些糖和编写array[index]
得到完全相同的结果,这更加清晰。@同样清晰地展开。参数中double*的含义是什么?(并且不起作用?)指向指针的指针,例如指向字符数组数组的指针。您当前拥有的是一个指向(可能的)字符数组的指针,它只是一个字符串。参数中double*的含义是什么?(并且不起作用?)指向指针的指针,例如指向字符数组数组的指针。您当前拥有的是一个指向(可能的)字符数组的指针,它只是一个字符串。谢谢,我修复了它。谢谢,我修复了它。