簡體   English   中英

打印數組指針的C函數?

[英]C function that prints pointer of an array?

我正在嘗試制作一個可以打印出數組指針的交流程序,這是我嘗試過的

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

函數不起作用,因此沒有任何輸出

代碼中存在類型不匹配。 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);
 void  printArr(int index,char **arr){
     printf("%s\n",*(arr+index));
 }

自己玩https://ideone.com/LrNmJk

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM