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;  

  • 相关阅读:
    BZOJ2330 SCOI2011糖果
    BZOJ 3812主旋律
    模板更新 扩展卢卡斯
    BZOJ4556 HEOI2016字符串
    CodeForces700E Cool Slogans
    Atcoder Contest069F:Flag
    计算几何模板(更新中......)
    BZOJ4003 JLOI2015城池攻占
    BZOJ3772精神污染
    HDU5919 SequenceⅡ
  • 原文地址:https://www.cnblogs.com/wxb20/p/6150598.html
Copyright © 2011-2022 走看看