zoukankan      html  css  js  c++  java
  • Pointer in C++

    Pointer Variable Declarations and Initialization

    Pointer variables contain memory address as their values.

    Diagrams typically represent a pointer as an arrow from the variable that contains an address to the variable located at that address in memory.

    int *countPtr;
    

    Pointer should be initialized to 0, NULL or an address of the corresponding type either when they're declared or in an assignment.

    A pointer with the value 0 or NULL "points to nothing" and is known as a null pointer.

    Pointer Operators

    The address operator(&) is a unary operator that obtains the address memory address of its operand.

    int y = 5;  // declare variable y
    int *yPtr;  // declare pointer variable yPtr
    yPtr = &y;  // assign address of y to yPtr
    

    Now, yPtr indirectly references variable y's value.

    The * operator, commonly referred to as the indirection operator or dereferencing operator.

    indirection operator

    int y = 1;
    int *yPtr = &y;
    

    the value of yPtr is address of variable y.

    dereferencing operator

    *yPtr = 9;
    

    would assign 9 th variable y.

    Pointer and Array

    Assume the following declarations:

    int b[5];  // create 5-element int array b
    int *bPtr; // create int pointer bPtr
    

    Because the array name is a pointer to the first element of the array, we can set bPtr to the address of the first element in array b with the statement

    bPtr = b;  // assign address of the array b to bPtr
    

    also

    bPtr = &b[0];  // also assigns address of array b to bPtr
    

    Example

    #include <iostream>
    using namespace std;
    
    int main()
    {
      	int b[] = {10, 20, 30, 40};  // create 4-element array b
      	int *bPtr = b;  // set bPtr to point to array b
      
      	// output array b using array subscript notation
      	cout << "Array b printed with:
    
    Array subscript notation
    ";
      	for (int i = 0; i < 4; i++)
        {
          	cout << "b[" << i << "] = " << b[i] << "
    ";
        }
      
      	// out array b using the array name and pointer/offset notation
      	cout << "
    Pointer/offset notation where "
          	<< "the pointer is the array name
    ";
      	for (int offset1 = 0; offset1 < 4; offset1++)
        {
          	cout << "*(b + " << offset1 << ") = " << *(b + offset1) << "
    ";
        }
      
      	// output array b using bPtr and array subscript notation
      	cout << "
    Pointer subscript notation
    ";
      	for (int j = 0; j < 4; j++)
        {
          	cout << "bPtr[" << j << "] = " << bPtr[j] << "
    ";
        }
      
      	cout << "
    Pointer/offset notation
    ";
      
      	// out array b using bPtr and pointer/offset notation
      	for (int offset2 = 0; offset2 < 4; offset2++)
        {
          	cout << "*(bPtr + " << offset2 << ") = "
              	<< *(bPtr + offset2) << "
    ";
        }
    }
    

    output:

    Array b printed with:
    
    Array subscript notation
    b[0] = 10
    b[1] = 20
    b[2] = 30
    b[3] = 40
    
    Pointer/offset notation where the pointer is the array name
    *(b + 0) = 10
    *(b + 1) = 20
    *(b + 2) = 30
    *(b + 3) = 40
    
    Pointer subscript notation
    bPtr[0] = 10
    bPtr[1] = 20
    bPtr[2] = 30
    bPtr[3] = 40
    
    Pointer/offset notation
    *(bPtr + 0) = 10
    *(bPtr + 1) = 20
    *(bPtr + 2) = 30
    *(bPtr + 3) = 40
    Program ended with exit code: 0
    
  • 相关阅读:
    数据结构和算法(Golang实现)(14)常见数据结构-栈和队列
    数据结构和算法(Golang实现)(20)排序算法-选择排序
    数据结构和算法(Golang实现)(18)排序算法-前言
    数据结构和算法(Golang实现)(22)排序算法-希尔排序
    数据结构和算法(Golang实现)(21)排序算法-插入排序
    数据结构和算法(Golang实现)(27)查找算法-二叉查找树
    关于SpringMVC映射模型视图的几点小事
    关于spring中事务管理的几件小事
    关于spring中AOP的几件小事
    关于spring中bean配置的几件小事
  • 原文地址:https://www.cnblogs.com/okadanana/p/5890927.html
Copyright © 2011-2022 走看看