Skip to content

Latest commit

 

History

History
167 lines (118 loc) · 3.76 KB

数组和函数.md

File metadata and controls

167 lines (118 loc) · 3.76 KB

数组和函数

c语言中一共有两种聚合类型:数组(array)和结构(structure)

**标量(scalar)**具有保存单一数据项的能力。

**聚合变量(aggregate)**可以存储一组一组的数值

数组

数组名做右值使用时,自动转换成指向数组首元素的指针。

函数

基本概念

  • 形式参数(parameter)

  • 实际参数(argument)

  • 函数声明(function declaration)

  • 函数原型(function prototype)

函数返回的退出

  • return

    指定将要返回的值

  • exit

    exit 属于 <stdlib.h> 头。

    传递给exit的实际参数和main函数的返回值具有相同的含义

  • return 和 exit 区别

    exit 函数不论被哪个函数调用,都会导致程序终止;return 仅当main函数调用时才会导致程序终止。

构建程序推荐编排顺序

  1. # include 指令
  2. # define 指令
  3. 类型定义
  4. 外部变量声明
  5. 函数原型声明
  6. main 函数定义
  7. 其他函数定义

一维数组和二维数组的初始化和显示

  • 完整代码

    /*****************************************************************
     * Name: array.cpp                                               *
     * Function:                                                     *
     * Author: Alex Chung                                            *
     * Time: 2020/7/21 PM 20:04                                      *
     * **************************************************************/
    
    #include <stdio.h>
    
    #define ROW_LEN 2
    #define COL_LEN 3
    
    //void show_one_pointer(int * a,  int size);
    void show_one_pointer(int * ,  int );
    //void show_one_array(int a [], int size);
    void show_one_array(int [], int ); // omit parameter name in typeproto
    
    //void show_two_pointer(int * a,  int row_size, int col_size);
    void show_two_pointer(int * ,  int , int );//
    //void show_tow_array(int a[][COL_LEN], int row);
    void show_tow_array(int [][COL_LEN], int); // omit parameter name in typeproto
    
    int main(void){
        
        // initial array
        int a_0[5] = {1, 2, 3, 4, 5};
        int a_1 [] = {5, 4, 3, 2, 1};
        int a_2 [5] = {1, 2, 3};
        // int a_3 [10] = {[1]=2, [3]=4, [4]=5}; C99
        // show a_0
         printf("a_0:\n");
        int size_0= sizeof(a_0) / sizeof(a_0[0]);
        show_one_pointer(a_0, size_0);
        // show a_1
        printf("a_1:\n");
        int size_1 = sizeof(a_1) / sizeof(a_1[0]);
        show_one_array(a_1, size_1);
    
        // multi dimension array
        int b_0[ROW_LEN][COL_LEN] = {1, 2, 3, 4, 5, 6};
        int b_1[][COL_LEN] = {6, 5, 4, 3, 2, 1};
        
        // show b_0
        printf("b_0:\n");
        int raw_size_0 = sizeof(b_0) / sizeof(b_0[0]);
        int col_size_0 = sizeof(b_0[0]) / sizeof(b_0[0][0]);
        show_two_pointer(&b_0[0][0], raw_size_0, col_size_0);
        // show b_1
        printf("b_1:\n");
        show_tow_array(b_1, ROW_LEN);
    
        return 0;
    }
    
    
    void show_one_pointer(int * a, int size){
    
        for (int i=0; i<size; i++ ){
            printf("%d ", a[i]);
        }
        printf("\n");
    }
    
    void show_one_array(int a [], int size){
    
        for (int i=0; i<size; i++ ){
            printf("%d ", a[i]);
        }
        printf("\n");
    }
    
    
    void show_two_pointer(int * a,  int row, int col){
    
        for (int i=0; i<row; i++ ){
            for (int j=0; j<col; j++){
                int * address = a + i*row + j;
                printf("%d ", * address);
            }
            printf("\n");   
        }
    
    }
    
    void show_tow_array(int a[][COL_LEN], int row){
        for (int i=0; i<row; i++){
            for (int j=0; j<COL_LEN; j++){
                printf("%d ", a[i][j]);
                }
        printf("\n"); 
        }
    }
  • 输出结果

    a_0:
    1 2 3 4 5
    a_1:
    5 4 3 2 1
    b_0:
    1 2 3
    3 4 5
    b_1:
    6 5 4
    3 2 1