zoukankan      html  css  js  c++  java
  • 多维数组和多维指针实例

    1.#include <stdio.h>

    int main(int argc, char* argv[], char* env[])
    {
        int a[3][3] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};
        int i = 0;
        int j = 0;
        
        for(i=0; i<3; i++)
        {
            for(j=0; j<3; j++)
            {
                printf("%d ", *(*(a+i) + j));
            }
        }
    }

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

    void printArray(int a[], int size)
    {
        int i = 0;
        
        printf("printArray: %d ", sizeof(a));

        for(i=0; i<size; i++)
        {
            printf("%d ", a[i]);
        }
    }

    int main()
    {
        int a[3][3] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};
        int* p = &a[0][0];
        
        printArray(p, 9);
        
        return 0;
    }
    3.动态分配二维数组
    #include <stdio.h>  
    #include <stdlib.h>  
    #include <malloc.h>
    #define Malloc(type,n)  (type *)malloc((n)*sizeof(type))  
     
    int main(int argc, char **argv)  
    {  
        int **array;  
        int i,j,row, column;  
        if(argc!=3)  
        {  
            printf("Run me with 2 parameters--rows & columns. For example: %s 3 4 returns 3 rows 4 columns array ", argv[0]);  
            exit(1);  
        }  
        row = atoi(argv[1]);  
        column = atoi(argv[2]);  
        array=Malloc(int *, row);  
        for(i=0;i<row;i++)  
        {  
            array[i]=Malloc(int, column);  
        }  
        //validation  
        for(i=0;i<row;i++)  
        {  
            for(j=0;j<column;j++)  
            {  
                array[i][j]=i;  
                printf("%4d", array[i][j]);  
            }  
            printf(" ");  
        }  
        for(i=0;i<row;i++)  
            free(array[i]);  
        free(array);  
        return 0;  

  • 相关阅读:
    tectangular container
    WIFI
    微信小程序动态改变数组或对象中的某个属性值
    常用的正则表达式
    前端登录通过账号显示对应头像
    JS返回页面时自动回滚到历史浏览位置
    JavaScript让登录或搜索文本框自动获得焦点
    react脚手架应用
    npm教程3_脚手架原理以及bootstrap引入
    npm教程2
  • 原文地址:https://www.cnblogs.com/wxb20/p/6150598.html
Copyright © 2011-2022 走看看