首页 > 编程语言 > 详细

【c】数组和指针的区别

时间:2020-05-10 12:41:07      阅读:35      评论:0      收藏:0      [点我收藏+]

数组和指针的区别

  • 数组是特殊的指针
  • 当数组名做为数组定义的标识符、sizeof或&操作时,表示整个数组;其他表达式时,表示指向第0个元素的指针。
#include <stdio.h>

int main(void) {
    int a[5] = {1, 2, 3};
    int len = sizeof(a) / sizeof(*a); // 数组 指针
    int *b = &a[1]; // 数组
    int *p = a; // 指针
    printf("%d %d %d", len, *b, *p); // 5 2 1
    return 0;
}

结构体中的数组

#include <stdio.h>

typedef struct tagStudent {
    int cores[4];
    int age;
} Student;

int main(void) {
    Student *stu;
    stu->cores[0] = 1;
    printf("%d\n", sizeof(*stu)); //20
    Student *stu2;
    memcpy(stu2, stu, sizeof(Student));
    printf("%d\n", stu2->cores[0]); // 1
    stu->cores[0] = 3;
    printf("%d\n", stu2->cores[0]); // 1
}

【c】数组和指针的区别

原文:https://www.cnblogs.com/kunlingou/p/12862447.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!