📜  指针在C / C++中的应用

📅  最后修改于: 2021-05-30 10:04:58             🧑  作者: Mango

先决条件:C / C++中的指针,C程序的内存布局。

  • 通过引用传递参数。通过引用有两个目的

(i)修改其他函数的变量。交换两个变量的示例;

C
// C program to demonstrate that we can change
// local values of one function in another using pointers.
 
#include 
 
void swap(int* x, int* y)
{
    int temp = *x;
    *x = *y;
    *y = temp;
}
 
int main()
{
    int x = 10, y = 20;
    swap(&x, &y);
    printf("%d %d\n", x, y);
    return 0;
}


C++
// C++ program to demonstrate that we can change
// local values of one function in another using
// pointers.
#include 
using namespace std;
 
void swap(int* x, int* y)
{
    int temp = *x;
    *x = *y;
    *y = temp;
}
 
int main()
{
    int x = 10, y = 20;
    swap(&x, &y);
    cout << x << " " << y << endl;
    return 0;
}


C
// C program to demonstrate that compiler
// internally uses pointer arithmetic to access
// array elements.
 
#include 
 
int main()
{
    int arr[] = { 100, 200, 300, 400 };
 
    // Compiler converts below to *(arr + 2).
    printf("%d ", arr[2]);
 
    // So below also works.
    printf("%d\n", *(arr + 2));
 
    return 0;
}


C++
// C++ program to demonstrate that compiler
// internally uses pointer arithmetic to access
// array elements.
#include 
using namespace std;
 
int main()
{
    int arr[] = { 100, 200, 300, 400 };
 
    // Compiler converts below to *(arr + 2).
    cout << arr[2] << " ";
 
    // So below also works.
    cout << *(arr + 2) << " ";
 
    return 0;
}


C
// C program to demonstrate that using a pointer
// we can return multiple values.
 
#include 
#include 
 
void fun(int n, int* square, double* sq_root)
{
    *square = n * n;
    *sq_root = sqrt(n);
}
 
int main()
{
 
    int n = 100;
    int sq;
    double sq_root;
    fun(n, &sq, &sq_root);
 
    printf("%d %f\n", sq, sq_root);
    return 0;
}


C++
// C++ program to demonstrate that using a pointer
// we can return multiple values.
#include 
using namespace std;
 
void fun(int n, int* square, double* sq_root)
{
    *square = n * n;
    *sq_root = sqrt(n);
}
 
int main()
{
 
    int n = 100;
    int* sq = new int;
    double* sq_root = new double;
    fun(n, sq, sq_root);
    cout << *sq << " " << *sq_root;
    return 0;
}


C
// C program to dynamically allocate an
// array of given size.
 
#include 
#include
int* createArr(int n)
{
    int* arr = (int*)(malloc(n * sizeof(int)));
    return arr;
}
 
int main()
{
    int* pt = createArr(10);
    return 0;
}


C++
// C++ program to dynamically allocate an
// array of given size.
#include 
using namespace std;
 
int* createArr(int n)
{
    return new int[n];
}
 
int main()
{
    int* pt = createArr(10);
    return 0;
}


输出 :

20 10

(ii)出于效率目的。在没有参考的情况下传递大型结构的示例将创建该结构的副本(因此浪费了空间)。
注意:以上两个也可以通过C++中的References来实现。

  • 用于访问数组元素。编译器内部使用指针访问数组元素。

C

// C program to demonstrate that compiler
// internally uses pointer arithmetic to access
// array elements.
 
#include 
 
int main()
{
    int arr[] = { 100, 200, 300, 400 };
 
    // Compiler converts below to *(arr + 2).
    printf("%d ", arr[2]);
 
    // So below also works.
    printf("%d\n", *(arr + 2));
 
    return 0;
}

C++

// C++ program to demonstrate that compiler
// internally uses pointer arithmetic to access
// array elements.
#include 
using namespace std;
 
int main()
{
    int arr[] = { 100, 200, 300, 400 };
 
    // Compiler converts below to *(arr + 2).
    cout << arr[2] << " ";
 
    // So below also works.
    cout << *(arr + 2) << " ";
 
    return 0;
}

输出 :

300 300
  • 返回多个值。返回数字的平方和平方根的示例。

C

// C program to demonstrate that using a pointer
// we can return multiple values.
 
#include 
#include 
 
void fun(int n, int* square, double* sq_root)
{
    *square = n * n;
    *sq_root = sqrt(n);
}
 
int main()
{
 
    int n = 100;
    int sq;
    double sq_root;
    fun(n, &sq, &sq_root);
 
    printf("%d %f\n", sq, sq_root);
    return 0;
}

C++

// C++ program to demonstrate that using a pointer
// we can return multiple values.
#include 
using namespace std;
 
void fun(int n, int* square, double* sq_root)
{
    *square = n * n;
    *sq_root = sqrt(n);
}
 
int main()
{
 
    int n = 100;
    int* sq = new int;
    double* sq_root = new double;
    fun(n, sq, sq_root);
    cout << *sq << " " << *sq_root;
    return 0;
}

输出 :

10000 10
  • 动态内存分配 :我们可以使用指针来动态分配内存。动态分配内存的优点是,直到我们显式删除它,才将其删除。

C

// C program to dynamically allocate an
// array of given size.
 
#include 
#include
int* createArr(int n)
{
    int* arr = (int*)(malloc(n * sizeof(int)));
    return arr;
}
 
int main()
{
    int* pt = createArr(10);
    return 0;
}

C++

// C++ program to dynamically allocate an
// array of given size.
#include 
using namespace std;
 
int* createArr(int n)
{
    return new int[n];
}
 
int main()
{
    int* pt = createArr(10);
    return 0;
}
  • 实现数据结构。
    链表,树等示例。由于引用固定在某个位置,因此我们不能使用C++引用来实现这些数据结构(例如,我们不能使用引用遍历链表)
  • 在内存地址有用的情况下进行系统级编程。例如,多个线程使用的共享内存。有关更多示例,请参见通过共享内存进行IPC,C / C++中的套接字编程等。
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。