zoukankan      html  css  js  c++  java
  • 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");//成功修改
    }
  • 相关阅读:
    zbb20181207 springboot @ConfigurationProperties使用
    zbb20181206 logback,lombok 默认日志logback配置解析
    Spring Boot (8) 全局异常处理
    Spring Boot (7) JdbcTemplate访问数据库
    Spring Boot (6) Spring Data JPA
    Spring Boot (4) 静态页面和Thymeleaf模板
    Spring Boot (3) 热部署devtools
    Spring Boot (2) Restful风格接口
    Spring Boot (1) 构建第一个Spring Boot工程
    idea使用maven搭建ssm框架实现登陆商品增删改查
  • 原文地址:https://www.cnblogs.com/cart55free99/p/2978449.html
Copyright © 2011-2022 走看看