首页 > 解决方案 > 打印数组指针的C函数?

问题描述

我正在尝试制作一个打印出数组指针的 ac 程序,这是我尝试的

#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));
}

功能不起作用它没有输出结果

标签: c

解决方案


代码中存在类型不匹配。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);

推荐阅读