C-当把数组传递给函数

无人久伴 提交于 2020-01-08 14:39:09
#include <stdio.h>

/**
 * C语言中,数组的名称就是 一连串连续内存的起始地址,
 * 因此给数组传递给函数,传递的就是数组元素类型的指针
*/

void hello_0(char msg[20]);
void hello_1(char msg[]);
void hello_2(char *msg);

int main() {

    char msg[20] = "hello kitty";
    char notice[40] = "this is a notice to kitty.";
    hello_0(msg);
    hello_1(msg);
    hello_2(msg);

    hello_0(notice);
    hello_1(notice);
    hello_2(notice);
    return 0;
}

void hello_0(char msg[20]) {
    printf("0 msg is [%s]\n", msg);
}

void hello_1(char msg[]) {
    printf("1 msg is [%s]\n", msg);
}

void hello_2(char *msg) {
    printf("2 msg is [%s]\n", msg);
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!