吾爱程序员:这里有好玩的游戏和软件
当前位置:首页C语言教程 → C 和 C++ 中的指针简介

C 和 C++ 中的指针简介

来源:网络 | 更新时间:2022-01-15 20:34:12
指针存储变量的地址或内存位置。 
// 通用语法
datatype *var_name; 
// 一个示例指针“ptr”,它持有
// 一个整数变量的地址或持有
// 一个内存的地址,其值可以
// 通过“ptr”作为整数值访问
int *ptr; 
使用指针: 要在 C 中使用指针,我们必须了解以下两个运算符。 
  • 将变量的地址赋值给指针,我们使用返回该变量地址的一元运算符& (与号)。例如 &x 给了我们变量 x 的地址。 
// The output of this program can be different
// in different runs. Note that the program
// prints address of a variable and a variable
// can be assigned different address in different
// runs.
#include <stdio.h>

int main()
{
int x;

// Prints address of x
printf("%p", &x);

return 0;
}
另外一个运算符是 *(星号),它用于两件事: 
  • 声明指针变量:在 C/C++ 中声明指针变量时,其名称前必须有一个 *。 
// C program to demonstrate declaration of
// pointer variables.
#include <stdio.h>
int main()
{
int x = 10;

// 1) Since there is * in declaration, ptr
// becomes a pointer variable (a variable
// that stores address of another variable)
// 2) Since there is int before *, ptr is
// pointer to an integer type variable
int *ptr;

// & operator before x is used to get address
// of x. The address of x is assigned to ptr.
ptr = &x;

return 0;
}
  • 要访问存储在地址中的值,我们使用一元运算符 (*),它返回位于其操作数指定的地址的变量的值。这也称为取消引用
// C program to demonstrate use of * for pointers in C
#include <stdio.h>

int main()
{
// A normal integer variable
int Var = 10;

// A pointer variable that holds address of var.
int *ptr = &Var;

// This line prints value at address stored in ptr.
// Value stored is value of variable "var"
printf("Value of Var = %dn", *ptr);

// The output of this line may be different in different
// runs even on same machine.
printf("Address of Var = %pn", ptr);

// We can also use ptr as lvalue (Left hand
// side of assignment)
*ptr = 20; // Value at address is now 20

// This prints 20
printf("After doing *ptr = 20, *ptr is %dn", *ptr);

return 0;
}
输出 :
Value of Var = 10
Address of Var = 0x7fffa057dd4
After doing *ptr = 20, *ptr is 20
以下是上述程序的图示:  指针表达式和指针算术 可以对指针执行一组有限的算术运算。指针可能是: 
  • 递增 (++)
  • 递减(—)
  • 可以将整数添加到指针( + 或 += )
  • 可以从指针中减去整数( – 或 -= )
除非在数组上执行,否则指针算术是没有意义的。  注意:指针包含地址。添加两个地址没有意义,因为不知道它会指向什么。减去两个地址可以计算这两个地址之间的偏移量。
// C++ program to illustrate Pointer Arithmetic
// in C/C++
#include <bits/stdc++.h>

// Driver program
int main()
{
// Declare an array
int v[3] = {10, 100, 200};

// Declare pointer variable
int *ptr;

// Assign the address of v[0] to ptr
ptr = v;

for (int i = 0; i < 3; i++)
{
printf("Value of *ptr = %dn", *ptr);
printf("Value of ptr = %pnn", ptr);

// Increment pointer ptr by 1
ptr++;
}
}
Output:Value of *ptr = 10
Value of ptr = 0x7ffcae30c710

Value of *ptr = 100
Value of ptr = 0x7ffcae30c714

Value of *ptr = 200
Value of ptr = 0x7ffcae30c718
数组名称作为指针  数组名称的作用类似于指针常量。这个指针常量的值是第一个元素的地址。  例如,如果我们有一个名为 val 的数组,那么val&val[0]可以互换使用。 

// C++ program to illustrate Array Name as Pointers in C++
#include <bits/stdc++.h>
using namespace std;

void geeks()
{
// Declare an array
int val[3] = { 5, 10, 15};

// Declare pointer variable
int *ptr;

// Assign address of val[0] to ptr.
// We can use ptr=&val[0];(both are same)
ptr = val ;
cout << "Elements of the array are: ";
cout << ptr[0] << " " << ptr[1] << " " << ptr[2];

return;
}

// Driver program
int main()
{
geeks();
return 0;
}
Output:
Elements of the array are: 5 10 15
现在,如果将此 ptr 作为参数发送到函数,则可以以类似的方式访问数组 val。    指针和多维数组  考虑二维数值数组的指针表示法。考虑以下声明 
int nums[2][3] = { {16, 18, 20}, {25, 26, 27} };
一般来说,nums[i][j] 等价于 *(*(nums+i)+j)
指针 数组
*(*nums) nums[0][0] 16
*(*nums + 1) nums[0][1] 18
*(*nums + 2) nums[0][2] 20
*(*(nums + 1)) nums[1][0] 25
*(*(nums + 1) + 1) nums[1][1] 26
*(*(nums + 1) + 2) nums[1][2] 27
 

最新文章

热点资讯

手游排行榜

CopyRight 2020-2030吾爱程序员

鄂ICP备2021004581号-8

本站资源收集于网络,如有侵权请联系我们:35492删除0109@qq.com