当前位置:网站首页>c语言指针基本知识要点总结(一)
c语言指针基本知识要点总结(一)
2022-07-26 03:14:00 【BSP初级小学僧】
博客主页:https://blog.csdn.net/weixin_46094737?type=blog
欢迎评论留言 如有错误敬请指正!
本文由小学生廉原创,首发于 CSDN
未来很长,值得我们全力奔赴更美好的生活!
什么是指针?
指针是一个变量,其值为另一个变量的地址,即内存位置的直接地址。就像其他变量或常量一样,必须在使用指针存储其他变量地址之前,对其进行声明。指针变量声明的一般形式为:
#include <stdio.h>
int main()
{
int *num_01;
int a=10010;
num_01=&a;
printf("指针num_01的值为:%d\n",*num_01);
printf("指针num_01的地址为:%#p",*num_01);
return 0;
} 
在这里,int就是指针的基本类型(int型),它必须是一个有效的 C 数据类型,num01是指针变量的名称。用来声明指针的星号 * 与乘法中使用的星号是相同的。但是,在这个语句中,星号是用来指定一个变量是指针。
所有指针的值的实际数据类型,不管是整型、浮点型、字符型,还是其他的数据类型,都是一样的,都是一个代表内存地址的长的十六进制数。不同数据类型的指针之间唯一的不同是,指针所指向的变量或常量的数据类型不同。
指针变量的定义与使用
1.指针变量的定义
指针变量的定义格式为:
类型说明符 *指针变量名 [=初值];
要同时定义两个指针变量:
int *p1=NULL,*p2=NULL;
(NULL是c语言中预定义的空指针关键字,它作为一个特殊的指针,表示不指向任何对象)
定义指针变量时要注意以下几点:
(1)类型说明符表示该指针变量所指向的变量的数据类型。
(2)定义指针变量时,指针变量名前必须有一个“ * ”号,表示定义的变量是指针变量。
(3)指针变量在定义时允许对其初始化。
2.指针变量的使用
两个重要运算符:
(1)&:取地址运算符,如p=&a; 则p为变量a的地址。
(2):指针运算符,后面只能接指针变量。用于访问指针变量所指向的变量。如:*p表示访问指针变量p所指向的变量的内容。
练习使用指针交换两个变量的地址和数值
#include <stdio.h>
void temp(int *num01,int *num02);
int main()
{
int a=100,b=200,c;
int *p=NULL;
int *pa=&a;
int *pb=&b;
printf("数值交换前:\n");
printf("%d\n",a);
printf("%d\n",b);
printf("地址交换前:\n");
printf("%p\n",pa);
printf("%p\n",pb);
p=pa;
pa=pb;
pb=p;
printf("地址交换后:\n");
printf("%p\n",pa);
printf("%p\n",pb);
temp(&a,&b);//使用函数交换
printf("数值交换后:\n");
printf("%d\n",a);
printf("%d\n",b);
return 0;
}
void temp(int *num01,int *num02)
{
int p;
p=*num01;
*num01=*num02;
*num02=p;
}

指针与数组
一、指向一维数组元素的指针
c语言数组中的数组元素可以看作相应类型的变量。只要类型匹配,就可以定义一个指针变量,让这个变量存储数组中数组元素的地址,则该指针便指向该数组元素。
int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; //定义a为包含十个数据的数组
int *p; //p为指向整型变量的指针变量
p = &a[0];//把a[0]元素的地址赋给指针变量p
p=a;//与p = &a[0]等效,p的值是数组a首元素的地址通过指针引用一维数组元素:
1.指向一维数组首元素的指针
注意!!!
(1)可以使用*(p+i)访问元素a[i]。
(2)因为p和a都表示数组首地址,所以p+i也可以记作a+i,指向元素a[i]。
(3)指向数组的指针变量也可以带下标,如:p[i]与*(p+i)和*(a+i)等价,表示元素a[i]。
由此可知:当指针变量p指向一维数组a,即指向一维数组的第一个元素a[0]后,数组的第i+1个元素有以下4种写法:
a[i] ; p[i] ; *(a+i) ; *(p+i)
a[i]的地址也对应有4种写法:
&a[i] ; &p[i] ;a+i ; p+i ;
#include <stdio.h>
int main()
{
int *p;
int arr[5]={1,2,3,4,5};
int i;
p=&arr[0];//指向首地址arr[0]
printf("数组元素首地址的值为:%d\n",*p);
p=arr;//也可以这样定义指针p指向的数组首地址
printf("指针p的地址为:%#p\n",p);
for(i=0;i<5;i++)
{
printf("%d ",*p);
p++;
}
return 0;
}
边栏推荐
- QT signal transmission between multi-level objects signal transmission between multi-level nested class objects
- Summary of Huawei virtualization fusioncompute knowledge points
- 如何正确计算 Kubernetes 容器 CPU 使用率
- QT笔记——Q_Q 和Q_D 学习
- 称霸薪酬榜!什么行业大有“钱”途?
- 线性回归原理推导
- Unity quickly builds urban scenes
- Docker installs redis!!! (including detailed illustration of each step) actual combat
- 78. Subset
- [STL]优先级队列priority_queue
猜你喜欢

PXE高效批量网络装机

HCIP第十四天
![[tensorflow & pytorch] image data enhancement API](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[tensorflow & pytorch] image data enhancement API

Functions and usage of snownlp Library

PXE efficient batch network installation

Summary of Huawei virtualization fusioncompute knowledge points

Implement a method to find the sum of the number k and m in the array

ENVI_ Idl: create HDF5 file and write data (take writing GeoTIFF file to HDF file as an example) + detailed parsing
![[NOIP2001 普及组]装箱问题](/img/b7/1310b3e68d0ee016465fc069315af6.png)
[NOIP2001 普及组]装箱问题

UDP和TCP可以使用同一个端口吗?
随机推荐
如何正确计算 Kubernetes 容器 CPU 使用率
Design of golang lottery system
2020 AF-RCNN: An anchor-free convolutional neural network for multi-categoriesagricultural pest det
[untitled]
TCP experimental verification
[MySQL project practical optimization] complex trigger case sharing
Canvas - drawing pictures - dynamic drawing production
Unknown-Aware Object Detection:Learning What You Don’t Know from Videos in the Wild(CVPR 2022)
小测(一)
离线数据仓库从0到1-阶段二软件安装
事半功倍:学会WEB性能测试用例设计模型
Pit trodden when copying list: shallow copy and deep copy
els 消息循环
2022-07-21 第四小组 修身课 学习笔记(every day)
LeetCode·
Easyexcel sets row hiding to solve the problem of sethidden (true) invalidation
[STL]优先级队列priority_queue
Istio三之VirtualService、Gateway、DestinationRule配置使用
Get twice the result with half the effort: learn the web performance test case design model
URDF 语法详解