工具

关于细节安装参考:Window10上CLion极简配置教程
基本数据类型
数据类型 | 数据类型符 | 占用字节数 | 取值范围 |
---|---|---|---|
整型 | int | 2(或者4) | 同短/长整形 |
短整型 | short | 2 | -32768 ~ 32768 |
长整型 | long | 4 | -2^31 ~ 2^31 |
单精度实型 | float | 4 | -10^38 ~ 10^38 |
双精度实型 | double | 8 | -10^308 ~ 10^308 |
字符型 | char | 1 | -128 ~ 127 |
字符串 | char * | sizeof() | / |
常用的占用字节数
-
sizeof(int)
// 4 -
sizeof(double)
// 8 -
sizeof(char)
// 1
#include <stdio.h>
//基本数据类型
int main() {
int i = 100;
double d = 200;
float f = 200;
long l = 100;
short s = 100;
char c = 'a';
//字符串
char *str = "Clion";
//输出不能随便打印,需要占位
printf("i的值是:%d\n", i); // d == 整形
printf("d的值是:%lf\n", d); //lf == long float
printf("f的值是:%f\n", f); //f == float
printf("l的值是:%d\n", l); //d == 长整形
printf("s的值是:%d\n", s); //s == Short
printf("c的值是:%c\n", c); // c == char
printf("字符串是:%s\n", str); // s == String
printf("字符串地址是:%p \n", &str); // 取出地址
}
//编码为:GBK
i的值是:100
d的值是:200.000000
f的值是:200.000000
l的值是:100
s的值是:100
c的值是:a
字符串是:Clion
字符串地址是:000000000061FE14
指针
- 指针就地址,
指针 == 地址
- **& 取地址标识 , * 取值 **
- C、C++ 万物皆地址
函数、对象、基本数据类型、结构...
int number = 100;
* number //取值
&number //取地址
*(&number) //通过地址取值,
多级指针
#include <stdio.h>
int main() {
int num = 666;
int *num_p = #
int **num_p_p = &num_p;
int ***num_p_p_p = &num_p_p;
printf("num_p 的值是:%p , num_p_p的值是:%p \n", num_p, num_p_p);
printf("获取值:%d \n", num);
printf("获取值:%d \n", *num_p);
printf("获取值:%d \n", **num_p_p);
printf("获取值:%d \n", ***num_p_p_p);
return 0;
}
//输出:
num_p 的值是:000000000061FE14 , num_p_p的值是:000000000061FE08
获取值:666
获取值:666
获取值:666
获取值:666
多级指针:指针存放的是内存地址,但是自己也有内存地址
* XXX 取得是内存地址所对应的值
数组
#include <stdio.h>
int main() {
int num[] = {1, 2, 3, 4};
int i;
for (i = 0; i < 4; ++i) {
printf("当前的位置 %d , 当期的数值:%d \n", i, *(num + i));
printf("当期的内存地址 %p \n",&num+i);
}
return 0;
}
//输出:
当前的位置 0 , 当期的数值:1
当期的内存地址 000000000061FE00
当前的位置 1 , 当期的数值:2
当期的内存地址 000000000061FE10
当前的位置 2 , 当期的数值:3
当期的内存地址 000000000061FE20
当前的位置 3 , 当期的数值:4
当期的内存地址 000000000061FE30
**优先级:[] > () > ***
指针数组
- int * arr[5]
- 优先级:[] 的优先级比 * 高
- arr是一个数组,而 int * 是数组里面的内容
- arr是一个含有 5 和 int * 的数组

数组指针
- int (* arr) [5]
- 优先级:由于 [] 的优先级比 () 高,因此在写数组指针的时候必须将 *arr 用括号括起来
- arr 先和 * 结合,说P的一个指针变量
- 指针arr指向一个大小为5的整型的数组

指针计算
# include <stdio.h>
int main() {
int arrInt[] = {6, 4, 10, 3, 1, 2, 9, 7, 0, 5, 100, 88, 6};
int result = arrInt[*(arrInt + 2) + *(arrInt + 4)]; //求 result的值
int length = 0;
length = sizeof(arrInt) / sizeof(int);
printf("数组的长度为: %d\n", length);
//其中arrInt默认会指向数组第一个是0, ===》逻辑地址、物理地址
int a = *(arrInt + 2); // 取出 数组中第2个值:10
int b = *(arrInt + 4); // 取出 数组中第4个值:1
int c = arrInt[a + b]; // 取出 数组中第11个值为:88
printf("计算a :%d \n", a);
printf("计算b :%d \n", b);
printf("计算c :%d \n", c);
printf("result的值是 : %d,%d, %d \n", a, b, result);
if (arrInt[0] == arrInt[12]) {
printf("值 相等== %d,%d \n", arrInt[0], arrInt[12]);
}else{
printf("值 不相等== %d,%d \n", arrInt[0], arrInt[12]);
}
if (&arrInt[0] == &arrInt[12]) {
printf("物理地址 相等== %d,%d \n", &arrInt[0], &arrInt[12]);
} else {
printf("物理地址 不相等== %d,%d \n", &arrInt[0], &arrInt[12]);
}
printf("============== &物理地址 *指正 ======================\n");
int temp = 1000;
int temp1 = *(&temp);
int temp2 = *(&temp);
printf("值是 相等== %d,%d \n", &temp1, &temp2);
printf("值是 相等== %d,%d \n", *(&temp1), *(&temp2));
if (&temp1 == &temp2) {
printf("物理地址 相等== %d,%d \n", &temp1, &temp2);
} else {
printf("物理地址 不相等== %d,%d \n", &temp1, &temp2);
}
return NULL;
}
//输出 控制台
数组的长度为: 13
计算a :10
计算b :1
计算c :88
result的值是 : 10,1, 88
值 相等== 6,6
物理地址 不相等== 6421968,6422016
============== &物理地址 *指针 ======================
值是 相等== 6421960,6421956
值是 相等== 1000,1000
物理地址 不相等== 6421960,6421956
截取
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void substrAction(char **result, char *str, int start, int end) {
char *temp = str;
char resultArr[end - start];
int count = 0;
for (int i = start; i < end; ++i) {
resultArr[count] = *(temp + i); //给容器
count++;
}
// TODD 这里要用StrCpy 赋值, 不然函数结束会弹栈,导致main函数无法取值
// *result = resultArr;
strcpy(*result, resultArr);
printf("substrAction1: %s \n", *result);
}
/**
* 此函数, 自己开辟 栈堆大小
* @param result
* @param str
* @param start
* @param end
*/
void substrAction2(char **result, char *str, int start, int end) {
char *temp = str;
char *resultArr = malloc(end - start); //分配所需的内存空间,并返回一个指向它的指针
int count = 0;
for (int i = start; i < end; ++i) {
resultArr[count] = *(temp + i); //给容器
count++;
}
*result = resultArr;
printf("substrAction2:%s \n", resultArr);
// free(resultArr); //释放 指针指向为NUll,函数指针结束会弹栈
}
void substrAction3(char *result, char *str, int start, int end) {
for (int i = start; i < end; ++i) {
*(result++) = *(str + i);
}
}
void substrAction4(char *result, char *str, int start, int end) {
strncpy(result, str + start, end - start);
}
int main() {
char *str = "is NengLee";
char *result;
substrAction(&result, str, 2, 11);
printf("main 截取的数值是 %s \n", result);
printf("\n");
substrAction2(&result, str, 2, 11);
printf("main 截取的数值是 %s \n", result);
free(result); //这里销毁
printf("\n");
substrAction3(result, str, 2, 11);
printf("main 截取的数值是 %s \n", result);
printf("\n");
substrAction4(result, str, 2, 11);
printf("main 截取的数值是 %s \n", result);
return 0;
}
- malloc 开辟堆空间
- free 销毁堆空间
网友评论