一、开发环境:clion ,mingw64.
二、数据类型:
三、void数据类型
void字面意思是”无类型”,void* 无类型指针,无类型指针可以指向任何类型的数据。
void定义变量是没有任何意义的,当你定义void a,编译器会报错。
void真正用在以下两个方面:
- 对函数返回的限定;
- 对函数参数的限定;
#include <stdio.h>
#include <malloc.h>
/**
* 结构体:相当于java中的类对象
* */
typedef struct {
int a;
int b;
}Person;
void structTest(){
//C中只能用malloc手动给对象分配堆内存
Person *person = malloc(sizeof(Person));
person->a = 10;
person->b = 55;
printf("%d, %d\n",person->a,person->b);
}
/*
* 计算机三大总线:地址总线、数据总线、控制总线
* 指针是有类型的,定义什么类型就应该存什么类型
* */
void pointerTest(){
int *p;//定义一个指针类型的
int a = 10;//实体变量
p = &a;//将a的地址指向p
printf("%p , %p\n",&a, p);
printf("%d , %d\n",a,*p);
printf("指针的大小固定为:%d个字节\n",sizeof(p));
}
/*
* 指针是有步长的,比如int类型的指针一次+4个字节,char一次+1个字节
* 指针类型决定了步长
* */
void stepSizeTest(){
int *a;
char *b;
printf("步长:%d, %d\n",a,b);
printf("步长加一:%d, %d\n",a+1,b+1);
}
/*
* 数组是一个比较特殊的指针
* 数组的地址指向首个元素的地址:c == &c[0] == &c
* */
void arrayTest(){
int c[5] = {3,1,5,8,5};
printf("%d\n",c);
printf("%d\n",&c);
printf("%d\n",&c[0]);
}
/*
* 数组指针
* */
void arrayPointerTest(){
int a =10;
int b =20;
int c =40;
int *p1 = &a;
int *p2 = &b;
int *p3 = &c;
int *arr[3] = {p1,p2,p3};
// *arr == arr[0]
printf("%d\n",*arr[0]);
printf("%d\n",**arr);
}
int main() {
structTest();
pointerTest();
stepSizeTest();
arrayTest();
arrayPointerTest();
return 0;
}