C和C++中的指针

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

// General syntax
datatype *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;  

使用指针

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

1. 为了访问变量的指针地址,我们使用一元运算符&来返回该变量的地址。例如&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; 
} 

2. 还有一个运算符是一元*(星号),用于两种情况:

1) 声明指针变量:在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 varaible (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; 
}

2) 要访问存储在地址中的值,我们使用一元运算符(*),该运算符返回位于其操作数指定地址处的变量的值。

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

六一编程网

发表评论

电子邮件地址不会被公开。 必填项已用*标注

Next Post

Python中的决策(if,if..else,嵌套if,if-elif)

周日 3月 22 , 2020
在现实生活中,有时需要做出一些决定,然后根据这些决定来决定下一步该做什么。在编程中也会出现类似的情况 […]

You May Like