C和C++中的指针集1(介绍、算术和数组)

指针存储变量的地址或内存位置。

null
// General syntaxdatatype *var_name; // An example pointer "ptr" that holds// address of an integer variable or holds// address of a memory whose value(s) can// be accessed as integer values through "ptr"int *ptr;  

使用指针:

图片[1]-C和C++中的指针集1(介绍、算术和数组)-yiteyi-C++库

要在C中使用指针,我们必须了解以下两个运算符。

  • 要访问指向指针的变量地址,我们使用一元运算符 & (符号)返回该变量的地址。例如&x给出了变量x的地址。

C

// 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

// 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++

// C++ program to demonstrate use of * for pointers in C++
#include <iostream>
using namespace std;
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"
cout << "Value of Var = " << *ptr << endl;
// The output of this line may be different in different
// runs even on same machine.
cout << "Address of Var = " <<  ptr << endl;
// We can also use ptr as lvalue (Left hand
// side of assignment)
*ptr = 20; // Value at address is now 20
// This prints 20
cout << "After doing *ptr = 20, *ptr is " << *ptr << endl;
return 0;
}
// This code is contributed by
// shubhamsingh10


C

// 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 = %d" , *ptr);
// The output of this line may be different in different
// runs even on same machine.
printf ( "Address of Var = %p" , 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 %d" , *ptr);
return 0;
}


  • 输出:
Value of Var = 10Address of Var = 0x7fffa057dd4After doing *ptr = 20, *ptr is 20
  • 以下是上述程序的图示:

pointers in c

指针表达式和指针算法 可以对指针执行一组有限的算术运算。指针可以是:

  • 递增(++)
  • 递减(-)
  • 整数可以添加到指针(+或+=)
  • 整数可以从指针中减去(–或-=)

指针算术除非在数组上执行,否则毫无意义。 注意:指针包含地址。添加两个地址没有意义,因为不知道它会指向什么。减去两个地址可以计算这两个地址之间的偏移量。

CPP

// 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 = %d" , *ptr);
printf ( "Value of ptr = %p" , ptr);
// Increment pointer ptr by 1
ptr++;
}
}


Output:Value of *ptr = 10Value of ptr = 0x7ffcae30c710Value of *ptr = 100Value of ptr = 0x7ffcae30c714Value of *ptr = 200Value of ptr = 0x7ffcae30c718

Untitled presentation (3)

数组名作为指针 数组名的作用类似于指针常量。这个指针常量的值是第一个元素的地址。 例如,如果我们有一个名为val的数组 瓦尔 &val[0] 可以互换使用。

CPP

// 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

Untitled presentation (2)

现在,如果这个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

相关文章 :

C/C++中指针的应用。 测验 指针基础知识测验 , 高级指针测验 参考: https://www.ntu.edu.sg/home/ehchua/programming/cpp/cp4_PointerReference.html 本文由 Abhirav Kariya。 如果你喜欢GeekSforgeks并想贡献自己的力量,你也可以用write写一篇文章。极客。组织或邮寄你的文章进行评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享