zoukankan      html  css  js  c++  java
  • 学点 C 语言(22): 数据类型 多维数组与指针


    1. 关于数组的首地址:
    #include <stdio.h>
    
    int main(void)
    {
        char cs[2][3] = {
                          {'A','B','C'},
                          {'D','E','F'}
                        };
        char *p1,*p2,*p3,*p4;
    
        p1 = p2 = p3 = p4 = NULL;
    
        /* 下面四个指针都是指向了同一个地址 */
        p1 = &cs[0][0]; /* 这个最好理解 */
        p2 = &cs[0];
        p3 = &cs;
        p4 = cs;        /* 这个最方便   */
    
        printf("%p\n%p\n%p\n%p\n", p1, p2, p3, p4);    /* 显示地址 */
        printf("\n%c %c %c %c\n", *p1, *p2, *p3, *p4); /* 显示内容 */
    
        getchar();
        return 0;
    }
    

    2. 数组其他元素的地址:

    例子中, 数组的元素在内存中应该是这样排列的:

    [0][0] [0][1] [0][2] [1][0] [1][1] [1][2]

    下面是通过指针的方式获取数组的第三个元素:
    #include <stdio.h>
    
    int main(void)
    {
        int nums[2][3] = {
                           {11, 12, 13},
                           {21, 22, 23}
                         };
        int *p1,*p2;
        p1 = p2 = NULL;
    
        p1 = &nums[0][2];
        
        p2 = nums;
        p2 = p2 + 2;
    //    p2 = (int *)nums + 2; /* 或者用这一句替换上面两行 */
    
        printf("%d, %d\n", *p1, *p2);
    
        getchar();
        return 0;
    }
    

    3. 遍历数组的普通方法:
    #include <stdio.h>
    
    int main(void)
    {
        int nums[2][3] = {
                           {11, 12, 13},
                           {21, 22, 23}
                         };
        int i,j;
        for (i = 0; i < 2; i++) {
            for (j = 0; j < 3; j++) {
                printf("%d\n", nums[i][j]);
            }
        }
    
        getchar();
        return 0;
    }
    

    4. 通过指针遍历数组:
    #include <stdio.h>
    
    int main(void)
    {
        int nums[2][3] = {
                           {11, 12, 13},
                           {21, 22, 23}
                         };
        int *p = nums;
        
        int i;
        for (i = 0; i < 6; i++) {
            printf("%d\n", *(p+i));
        }
    
        getchar();
        return 0;
    }
    
    #include <stdio.h>
    
    int main(void)
    {
        char cs[2][3] = {
                          {'A','B','C'},
                          {'D','E','F'}
                        };
        char *p = cs;
        
        unsigned i;    
        for (i = 0; i < sizeof cs / sizeof cs[0][0]; i++) {
            printf("%c\n", *p++);
        }
    
        getchar();
        return 0;
    }
    
    #include <stdio.h>
    
    int main(void)
    {
        char cs[2][3] = {
                          {'A','B','C'},
                          {'D','E','F'}
                        };
        int i;
        for (i = 0; i < 6; i++) {
            printf("%c\n", *(*cs + i)); // *cs 是什么? 看下例
        }
    
        getchar();
        return 0;
    }
    

    5. 再探数组的指针地址:
    #include <stdio.h>
    
    int main(void)
    {
        char cs[2][3] = {
                          {'A','B','C'},
                          {'D','E','F'}
                        };
    
        //在本例中(二维数组)
        // cs 是指向数组 cs[0] 的地址
        // *cs 是指向 cs[0][0] 的地址
        printf("%p, %p\n", cs, *cs);
        
        // **cs 指向 cs[0][0] 的值
        printf("%c, %c\n", **cs, cs[0][0]);   
    
        getchar();
        return 0;
    }
    

    6. 使用指针遍历三维数组:
    #include <stdio.h>
    
    int main(void)
    {
        char cs[2][2][3] = {
                             {
                               {'A','B','C'},
                               {'D','E','F'}
                             },
                             {
                               {'1','2','3'},
                               {'4','5','6'}
                             }
                           };
        int i;
        int count = sizeof cs / sizeof cs[0][0][0];
    
        for (i = 0; i < count; i++) {
            printf("%c\n", *(**cs + i));
        }
    
        getchar();
        return 0;
    }
    

    7. 遍历多维数组还是用指针变量更容易理解:
    #include <stdio.h>
    
    int main(void)
    {
        char cs[2][2][3] = {
                             {
                               {'A','B','C'},
                               {'D','E','F'}
                             },
                             {
                               {'1','2','3'},
                               {'4','5','6'}
                             }
                           };
        char *p = (char *)cs;  /* 相对上面的例子, 这里加了类型转换; 这样编译器就没有提示了 */
                           
        int i;
        int count = sizeof cs / sizeof cs[0][0][0];
        for (i = 0; i < count; i++) {
            printf("%c\n", *p++);
        }
    
        getchar();
        return 0;
    }
    

  • 相关阅读:
    Mac zsh: command not found zsh 所有命令在终端失效
    Java根据FreeMarker模板生成Word(doc)文档(带图片)
    2021年Java面试总结——自我篇
    toArray转换踩坑 java.lang.ClassCastException
    并发和并行
    protoBuf3学习
    StringBuffer和StringBuilder区别
    深拷贝和浅拷贝
    从不订购的客户
    使用jenkins遇到的问题汇总
  • 原文地址:https://www.cnblogs.com/del/p/1342757.html
Copyright © 2011-2022 走看看