#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
int main0101()
{
//int arr[] = {1,2,3,4,5,6,7,8,'a','b','c'};//ok
int arr[] = {1,2,3,4,5,6,7,8,'a','b','c'};
//数组名是一个常量 不允许赋值
//数组名是数组首元素地址
//arr = 100; //error
int* p = arr;
printf("%p\n", p);
printf("%p\n", arr);
printf("%d\n", *p);
for (int i = 0; i < 10; i++)
{
//printf("%d\n", arr[i]);
//printf("%d\n", *(arr+1));//*(arr+1) 等效于 arr[1] 指针类型变量 + 1 等同于 内存地址 + sizeof(int)
//printf("%d\n", *(arr + i));//*(arr+i) 等效于 arr[i] 指针类型变量 + 1 等同于 内存地址 + sizeof(int)
//printf("%d\n", *(p + i));
//printf("%d\n", p[i]);//*(arr+1) 等效于 arr[1] 等效于 p[i]
printf("%d\n", *p++);
}
printf("%p\n", p);//00D7F71C
printf("%p\n", arr);//00D7F6F4 最终 p 与 arr相差40, 不在数组元素指针范围内了 成为了一个野指针
int step = p - arr;//
printf("%d\n", step);//10 两个指针的相减 得到的结果是两个指针的偏移量 为sizeof(int) 所有指针类型相减 ,得到的结果都是int类型
return 0;
}
int main0102()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int* p = arr;//p是指向数组的指针
//p[i] == *p(i+1)
//区别在于 p是变量 arr是长量
//p是一个指针,4个字节大小 arr是一个数组 ,40个字节大小
return 0;
}
//数组作为函数参数 会退化为指针,丢失数组的精度 sizeof(arr) =4 而不是40了 相当于传过来的int arr[] 变为了 int* arr
void maopaopao(int arr[], int len)
{
int temp;
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len-1-i; j++)
{在这里插入图片描述
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
/*if (*(arr+j) > *(arr+j+1))
{
temp = *(arr + j);
*(arr + j) = *(arr + j + 1);
*(arr + j + 1) = temp;
}*/ //不建议这种写法 麻烦
}
}
}
int main()
{
int arr[] = { 9,1,5,3,8,10,2,7,4 ,20};
maopaopao(arr, sizeof(arr) / sizeof(arr[0]));
for (int i = 0; i < 10; i++)
{
printf("%d\n", arr[i]);
}
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
void My_Strcopy(char* dest, char* str)
{
int i = 0;
//while (str[i] != '\0')
//while (str[i] != 0)//说明是真的值才可以进循环那就等效于如下
while (str[i])
{
dest[i] = str[i];
i++;
}
dest[i] = 0;
}
void My_Strcopy01(char* dest, char* str)
{
int i = 0;
while (*(str + i))
{
*(dest + i) = *(str + i);
i++;
}
*(dest + i) = 0;
}
void My_Strcopy02(char* dest, char* str)
{
while (*str)
{
*dest = *str;
dest++;
str++;
}
*dest = 0;
}
void My_Strcopy03(char* dest, char* str)
{
while (*dest++ = *str++);
}
int main0201()
{
char str[] = "hello world";
char dest[100];
//My_Strcopy(dest, str);
My_Strcopy01(dest, str);
printf("%s\n", dest);
return 0;
}
int main0202()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
//char* p;
int* p = arr;
//arr[-1]//数组下标越界
p = &arr[3];
//p--;//指针加减运算和指针类型有关
//p--;
//p--;
//内存地址相差是12/ sizeof(int) = 偏移量
//int step = p - arr;//结果为3
//指针操作数组允许下标是负数
printf("%d\n", p[-2]);//等效于*(p-2)
printf("%p\n", p);
printf("%p\n", arr);
printf("%d\n", p-arr);
return 0;
}
int main0203()
{
//指针和运算符的操作
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int* p = arr;
//p = p + arr;//error
//p = p * arr;//error
//p = p * 4;//error
//p = p / arr;//error
//p = p % 4;//error
//指针不允许,乘除,取余,指针加指针,允许加偏移,允许减偏移,允许减野指针但没意义
if (p > arr)
{
printf("真\n");
}
if (p && arr)
{
print("真");
}
printf("%s\n", dest);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
int main0301()
{
//定义数组 数据类型 数据名[元素个数] = {值1,值2,值3}
//int* arr[3];//定义指针数组
int a = 10;
int b = 20;
int c = 30;
int *arr[3] = { &a,&b,&c };
for (int i = 0; i < sizeof(arr)/ sizeof(arr[0]); i++)
{
printf("%d\n", *arr[i]);
}
printf("指针数组大小:%d\n", sizeof(arr));
printf("指针元素大小:%d\n", sizeof(arr[0]));
return 0;
}
int main0303()
{
//
//指针数组里面元素存储的都是指针
int a[] = { 1,2,3 };
int b[] = { 4,5,6 };
int c[] = { 7,8,9 };
int* arr[3] = { a,b,c }; //指针数组是一个特殊的二维数组模型 相当于一个二级指针模型
//指针数组对应于二级指针//指针数组对应于二级指针//指针数组对应于二级指针//指针数组对应于二级指针
//指针数组核二级指针建立关系
int** p = arr;
/*printf("%p\n", arr[0]);
printf("%p\n", a);
printf("%p\n", &a[0]);
printf("%d\n", arr[0][1]);*/
printf("%p\n", arr);//arr是指针数组首地址
printf("%p\n", &arr[0]);
printf("%p\n", arr[0]);
printf("%p\n", a);
for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
{
for (int j = 0; j < 3; j++)
{
//二维数组形式打印
//printf("%d", arr[i][j]);
//printf("%d", *(arr[i]+j));//形式二
printf("%d", *(*(arr+i)+j));//形式三
}
puts(" ");
}
printf("指针数组大小:%d\n", sizeof(arr));
printf("指针元素大小:%d\n", sizeof(arr[0]));
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
int main0401()
{
int a[] = { 1,2,3 };
int b[] = { 4,5,6 };
int c[] = { 7,8,9 };
int* arr[3] = { a,b,c }; //指针数组是一个特殊的二维数组模型 相当于一个二级指针模型
//指针数组对应于二级指针//指针数组对应于二级指针//指针数组对应于二级指针//指针数组对应于二级指针
//指针数组核二级指针建立关系
int** p = arr;
printf("%d\n", **p);// 相当于 arr[0][0] 以及 a[0]
//二级指针偏移量相当于跳过了一个一维数组
printf("%d\n", **(p+1));//----->4
//一级指针偏移量相当于跳过了一个元素
printf("%d\n", *(*p + 1)); //arr[0][1]--->2
printf("%d\n", *(*(p + 1)+1)); //arr[1][1]----->5
for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
{
for (int j = 0; j < 3; j++)
{
//二维数组形式打印
//printf("%d", p[i][j]);
//printf("%d", *(p[i]+j));//形式二
printf("%d", *(*(p + i) + j));//形式三
}
puts(" ");
}
return 0;
}
int main0402()
{
int a = 10;
int b = 20;
int* p = &a;
int** pp = &p;
int*** ppp = &pp;
//*ppp==pp==&p
//**ppp==*pp==p==&a
//***ppp==**pp==*p==&a
//*pp = &b;//等价于 p = &b;
**pp = 100;//等价于 a = 100;
// *pp = 100;//error 野指针
printf("%d\n", a);
printf("%d\n", *p);
printf("%d\n", **pp);
printf("%d\n", ***ppp);
return 0;
}
地址传递 函数形参改变实参的值
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
//void swap(int a, int b)
//{
// int temp = a;
// a = b;
// b = a;
// return;
//}
//指针作为函数参数传递
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
return;
}
int main()
{
int a = 10;
int b = 20;
//值传递 形参不影响实参的值
//swap(a, b);
//地址传递 形参可以改变实参的值
swap(&a, &b);
printf("%d\n", a);
printf("%d\n", b);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
void my_strcat1(char* a, char* b)
{
//strlen(a);
int i = 0;
while (a[i] != '\0')
{
i++;
}
/*printf("%d\n", i);
printf("%d\n", strlen(a));
*/
int j = 0;
while (b[j] != '\0')
{
a[i + j] = b[j];
j++;
}
a[i + j] = 0;
printf("%s\n", a);
}
void my_strcat2(char* a, char* b)
{
//strlen(a);
int i = 0;
while (*(a+i) != '\0')
{
i++;
}
/*printf("%d\n", i);
printf("%d\n", strlen(a));*/
int j = 0;
while (*(b+j) != '\0')
{
*(a+i + j) = *(b+j);
j++;
}
*(a + i + j) = 0;
printf("%s\n", a);
}
void my_strcat3(char* a, char* b)
{
//strlen(a);
/*while (*a)
{
a++;
}*/
/*while (*a++);
a--;*/
while (*a)a++;
while (*b)
{
*a = *b;
a++;
b++;
}
*a = 0;
}
void my_strcat4(char* a, char* b)
{
while (*a)a++;
while (*a++ = *b++);
}
int main()
{
char a[100] = "hello";
char b[] = "world";
my_strcat4(a, b);
//printf("%d\n", b);
printf("%s\n", a);
return 0;
}
字符串去空格
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>int main0601()
void remo_space1(char* a)
{
char b[100] = { 0 };
char* p = b;
int i = 0;
int j = 0;
while (a[i])
{
if (a[i] != ' ')
{
b[j] = a[i];
j++;
}
i++;
}
printf("%s\n", b);
while (*a++ = *p++);
return;
}
void remo_space2(char* a)
{
char b[100] = {0};
char* s = a;
char* p = b;
char* p1 = b;
while (*s)
{
if (*s != ' ')
{
*p = *s;
p++;
}
s++;
}
while (*a++ = *p1++);
}
void remo_space3(char* a)
{
char* p = a; //遍历字符串
char* pp = a;//记录非空格字符串
while (*p)
{
if (*p != ' ')
{
*pp = *p;
pp++;
}
p++;
}
*pp = 0;
}
int main()
{
char a[] = " h e l l o ";
remo_space2(a);
printf("%s\n", a);
return 0;
}
字符串查找字符
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
char* My_Search(char* a, char b)
{
while (*a)
{
if (*a == b)
{
return a;
}
a++;
}
return NULL;
}
int main()
{
char a[] = " h e l l o wolrd";
char* p = My_Search(a, 'l');
if (p == NULL)
{
printf("未找到\n");
}
else
{
printf("%s\n",p);
}
return 0;
}
字符串查找字符串
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
char* My_Search1(char* a, char* b )
{
char* fa = a;//记录相同字符串其实地址
char* ra = a;//遍历源字符串指针
char* tb = b;
while (*ra)
{
fa = ra;
while (*fa == *tb && *fa != '\0')
{
fa++;
tb++;
}
if (*tb == '\0')
{
return fa;
}
//回滚
tb = b;//目标字符串更新到起始位置
ra++;
}
return NULL;
}
int main()
{
char a[] = "hello w olrd";
char b[] = "llo";
char* p = My_Search1(a, b);
printf("%s\n", p);
}