C 数组作为参数

#include<stdio.h>
#include<string.h>
#include <malloc.h>

void f1(char s[], int n);
void f2(char *s);
void f3(char s[20][20]); //这里的大小要和实参中数组大小匹配  否则可能不会得到理想值
void f4(char **str);
void f5(char * str[]);


void main() {
    int i, n;
    char str[20][20] = { { "Adam" }, { "Bob" }, { "Dimen" }, { "Colin" }, {
            "Correal" }, { "Sick" }, { "Rachel" } };

    char * str1[20]= { { "Adam" }, { "Bobs" }, { "Dimen" }, { "Colin" },
            { "Correal" }, { "Sick" }, { "Rachel" } };//这种是定义字符串数组最好的方式


    //以str2作为实参 总是报错
    char **str2 = { { "Adam" }, { "Bob" }, { "Dimen" }, { "Colin" },
            { "Correal" }, { "Sick" }, { "Rachel" } };



    char s1[10] = { "ABCD" };
    char *s2={"NMJK"};
    f1(s1, 5);
    f1(s2, 5);
    f2(s1);
    f2(s2);
    f3(str);//传入的实参必须是char a[][]
    f4(str);//null
    //f4(str2);//报错
   f5(str);//null
    //f5(str2);//报错
    f5(str1);//ok


    printf(str1);


}

void f1(char s[], int n) { //虽然声明时用的是char s[]  但实现时char *s也是可以的
    //且实参 可以是char s[]  也可以是char *s
    printf("%s\n", s);
}

void f2(char s[]) { //很神奇!char *s  char s[] 等同效果
    printf("%s\n", s);
}

void f3(char str[20][20]) {
    printf("%c ,", str[2][2]); //m
    char (*p)[20] = str; //p指向str中的第一行
    printf("%s ,", *(p + 1)); //Bob
    //char **p=str;//这样定义是错误的

}

void f4(char ** str) {
    printf("\n");
    printf("%s ,", *(str + 1));
}

void f5(char * str[]){
    printf("\n");
    printf("%s ,", *(str + 1));
    printf("%s ,", *(str + 2));
    printf("%c ,", *(*(str)+3));//m
    printf("%c ,", *(*(str)+4));// '\0'
    printf("%c ,", *(*(str)+5));//B
    printf("%c ,", *(*(str)+6));//o
    printf("%s,",*(str)+1);//dam
    strcpy(str,"2003");//成功修改
}
原文地址:https://www.cnblogs.com/cart55free99/p/2978449.html