C语言学习 12(指针学习1)
一.内存和地址
1.内存
⼀楼:101,102,103...
⼆楼:201,202,203...
...
bit - ⽐特位
Byte - 字节
KB
MB
GB
TB
PB
1Byte = 8bit
1KB = 1024Byte
1MB = 1024KB
1GB = 1024MB
1TB = 1024GB
1PB = 1024TB
#include <stdio.h>
int main()
{
int a = 10;
&a;
printf("%p\n", &a);
return 0;
}
- 每运行一次,存储的地址就会发生一次改变。
#include <stdio.h>
int main()
{
int a = 10;
int* pa = &a; //取出a的地址,存入指针变量pa中。
return 0;
}
int a = 10;
int* pa = &a;
char ch = 'A';
char* pc = &ch;
4.解引用操作符(*)
#include <stdio.h>
int main()
{
int a = 10;
int* pa = &a;
*pa = 0;
printf("%d", a);
return 0;
}
打印结果:
#include <stdio.h>
//指针变量的⼤⼩取决于地址的⼤⼩
//32位平台下地址是32个bit位(即4个字节)
//64位平台下地址是64个bit位(即8个字节)
int main()
{
printf("%zd\n", sizeof(int*));
printf("%zd\n", sizeof(short*));
printf("%zd\n", sizeof(double*));
printf("%zd\n", sizeof(char*));
printf("%zd\n", sizeof(float*));
return 0;
}
x64环境(16进制)打印:
- 32位平台下地址是32个bit位,指针变量⼤⼩是4个字节
- 64位平台下地址是64个bit位,指针变量⼤⼩是8个字节
#include <stdio.h>
int main()
{
int a = 0x11223344;
int* pa = &a;
*pa = 0;
return 0;
}
第一段代码的内存改变的情况:
#include <stdio.h>
int main()
{
int a = 0x11223344;
char* pa = &a;
*pa = 0;
return 0;
}
第二段代码内存改变情况:
#include <stdio.h>
int main()
{
int a = 10;
int* pa = &a;
char* pc = &a;
printf("%p\n", &a);
printf("%p\n", pa);
printf("%p\n", pc);
printf("%p\n", pa + 1);
printf("%p\n", pc + 1);
return 0;
}
打印结果:
#include <stdio.h>
int main()
{
int a = 10;
int* pa = &a;
char* pc = &a;
return 0;
}
#include <stdio.h>
int main()
{
int a = 10;
void* pa = &a;
void* pc = &a;
*pa = 0;
*pc = 0;
return 0;
}
VS2022编译结果:
- 指针+- 整数
- 指针-指针
- 指针的关系运算
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
#include <stdio.h>
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
int* p = &arr[0];
int i = 0;
int sz = sizeof(arr) / sizeof(arr[0]);
for (i = 0; i < sz; i++)
{
printf("%d ", *(p + i));
}
return 0;
}
也可以将数组里的数全改为0;
#include <stdio.h>
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
int* p = &arr[0];
int i = 0;
int sz = sizeof(arr) / sizeof(arr[0]);
for (i = 0; i < sz; i++)
{
*(p + i) = 0;
}
for (i = 0; i < sz; i++)
{
printf("%d ", *(p + i));
}
return 0;
}
当然也可以自己输入,但输入的时候就不用加&了。
#include <stdio.h>
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
int i = 0;
int sz = sizeof(arr) / sizeof(arr[0]);
int* p = &arr[sz - 1];
for (i = 0; i < sz; i++)
{
*p = i + 1;
p--; //指针向前遍历
}
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
2.指针-指针
#include <stdio.h>
int main()
{
int arr[10] = { 0 };
int n = &arr[9] - &arr[0];
printf("%d", n);
return 0;
}
打印结果:
#include <stdio.h>
size_t my_strlen(char* str)
{
int count = 0;
while (*str != '\0')
{
str++;
count++;
}
}
int main()
{
char arr[] = "abcdef";
size_t len = my_strlen(arr);
printf("%zd", len);
return 0;
}
指针-指针形式:
#include <stdio.h>
size_t my_strlen(char* str)
{
char* start = str;
while (str != '\0')
{
str++;
}
return str - start;
}
int main()
{
char arr[] = "abcdef";
size_t len = my_strlen(arr);
printf("%zd", len);
return 0;
}
3.指针的关系运算
#include <stdio.h>
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
int* p = &arr[0];
int sz = sizeof(arr) / sizeof(arr[0]);
while (p < arr + sz)
{
printf("%d ", *p);
p++;
}
return 0;
}
原文地址:https://blog.csdn.net/2401_87671334/article/details/144060753
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!