【c】数组和指针的区别

数组和指针的区别

  • 数组是特殊的指针
  • 当数组名做为数组定义的标识符、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
", sizeof(*stu)); //20
    Student *stu2;
    memcpy(stu2, stu, sizeof(Student));
    printf("%d
", stu2->cores[0]); // 1
    stu->cores[0] = 3;
    printf("%d
", stu2->cores[0]); // 1
}
剑指 Offer
原文地址:https://www.cnblogs.com/kunlingou/p/12862447.html