
#include#include #include int main() { int a = 10; a = 20; int b = 100; //指针是一种数据类型 p是指针类型变量 用来指向一个变量的地址 int* p = &a; p = &b; printf("%pn", &b); printf("%Xn", p); //通过指针修改变量的值 *p = 100; printf("%dn", b); printf("%dn", *p); //sizeof()指针类型在内存中的大小 在32位操作系统中所有指针大小为4个字节大小 //在64位操作系统中所有指针大小为8个字节大小 printf("%dn", sizeof(p)); printf("int*=%dn", sizeof(int*)); printf("char*=%dn", sizeof(char*)); printf("short*=%dn", sizeof(short*)); printf("long*=%dn", sizeof(long*)); printf("float*=%dn", sizeof(float*)); printf("double*=%dn", sizeof(double*)); system("pause"); return EXIT_SUCCESS; }
指针类型在内存中大小:
指针指向变量和赋值操作:
野指针:
int main()
{
int a = 10;
int* p = &a;
//指向内存编号为100的内存地址
//0-255都是系统保留的 不可以读,但是不可以写
//野指针是指向一个未知的内存空间,可能在读写的时候出现错误
p = 100;
*p = 100;
system("pause");
return EXIT_SUCCESS;
}
会报错
空指针:
int main()
{
int* p;
//空指针就是指向内存编号为0的空间,操作该内存空间会报错,一般情况空指针用于程序条件判断
p = NULL;
*p = 100;
printf("%dn", *p);
if(p != NULL)
{
//free();
}
}
会报错
万能指针:
#include3 const修饰指针#include #include int main01() { int a = 10;//4 // int* p=&a; void* p = &a; //万能指针 *(int*)p = 100; //使用时得制定类型 printf("%dn", a); printf("%dn", *(int*)p); system("pause"); return EXIT_SUCCESS; } int main() { int arr[10] = { 0 }; void* p = arr; *(int*)p = 100;//arr[0]; arr[1] *((int*)p + 1) = 200;//arr[1] for (int i = 0; i < 10; i++) { printf("%dn", arr[i]); } return 0; }
1.通过指针修改const修饰的常量
#include#include #include #define LVL 100 int main() { //这种方式不安全,可以通过指针修改 //1.通过指针修改const修饰的常量 const int a = 10; printf("%dn", a); int* p = &a; *p = 100; printf("%dn", a); printf("%dn", *p); system("pause"); return EXIT_SUCCESS; }
2.如果const修饰int* 不能改变指针变量指向的内存地址的值,但是可以改变指针指向的地址
#include#include #include #define LVL 100 int main() { int a = 10; int b = 20; const int* p; p = &a; //但是可以改变指针指向的地址 p = &b; //如果const修饰int* 不能改变指针变量指向的内存地址的值 / char* mystrstr(char* dest, char* src) { int i = 0; int j = 0; //匹配个数 int count = 0; int len = strlen(src); char* p=NULL; while (dest[i] != ' ') { while (dest[i] == src[j]) { if (!count) //如果匹配成功一个字符,需要记录位置 p = &dest[i]; count++; i++; j++; //匹配成功 if (count == len) { return p; } } //发生改变的值 i j count p if (count < len) { i = i - count; j = 0; count = 0; } i++; } return NULL; } int main() { //char* p = strstr("hello world", "llo"); char* p = mystrstr("hello world", "llo"); printf("%sn", p); system("pause"); return EXIT_SUCCESS; }
strstr优化
#include14 指针和字符串char* mystrstr(char* dest, char* src) { char* p=NULL; char* temp = src; while (*dest) { p = dest; while (*dest == *temp) { dest++; temp++; } if (!*temp) return p; else temp = src; dest = p; dest++; } return NULL; } int main() { //char* p = strstr("hello world", "llo"); char* p = mystrstr("hello world", "llo"); printf("%sn", p); system("pause"); return EXIT_SUCCESS; }
字符串数组:
#include#include #include int main() { char arr[] = "hello world"; //栈区 char* p; p = arr; *p = 'A';//arr[0] p[0] p++; *p = 'B';//arr[1] p[1] printf("%sn", p); printf("%sn", arr); printf("%dn", sizeof(arr)); //12 包含 printf("%dn", strlen(arr)); //11 printf("%dn", sizeof(p)); //4 printf("%dn", strlen(p)); //10 system("pause"); return EXIT_SUCCESS; }
字符串常量:
#include#include #include int main() { char* arr = "hello world";//常量区 printf("%sn", arr); printf("%cn", arr[0]); char* p = arr; printf("%pn", p); //字符串常量是一个常量的数组,可以读取字符或者字符串,但不能修改 //p[0] = 'A';//报错 / //1.指针判断 //2.数组判断 //3.混合判断 if (arr[j][0] < arr[j + 1][0]) { char* temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } int main() { char* arr[] = { "dstudent","btree","cnew","abee" }; bubble(arr, 4); for (int i = 0; i < 4; i++) { printf("%sn", arr[i]); } system("pause"); return EXIT_SUCCESS; }