代码改变世界

指针数组,数据指针

  钟铧若岩  阅读(2)  评论(0编辑  收藏  举报
复制代码
#include <iostream>
using namespace std;



int main() {
    
    
    //指针数组
    int a1 = 1;
    int a2 = 2;
    int a3 = 3;
    int * a[3] = {&a1,&a2,&a3}; //指针数组
    
    cout << *a[0] << *a[1] << *a[2] << endl;
    //输出:123
    int **p = a; //指针的指针。
    cout << **p << endl;
    cout << **(p+1) << endl;
    cout << **(p+2) << endl;
    //输出:
    //1
    //2
    //3


   cout << *p << endl;
    cout << *(p+1) << endl;
    cout << *(p+2) << endl;
    //输出:
    //a1的物理地址
    //a2的物理地址
    //a3的物理地址

cout<< endl; int b[3] = {4,5,6}; int (*c)[3] = &b;//数组指针,指向数组地址的指针
  

    cout << (*c+0) << endl; //输出4对应的地址
    cout << (*c+1) << endl; //输出5对应的地址
    cout << (*c+2) << endl; //输出6对应的地址  这三个地址是连续的

    cout << *(*c+0) << endl;
    cout << *(*c+1) << endl;
    cout << *(*c+2) << endl;
    /*
    4
    5
    6
    */
    
    cout << endl;
    
    cout <<"c+0 = "<< (*c)[0]<<endl;
    cout <<"c+1 = "<< (*c)[1]<<endl;
    cout <<"c+2 = "<< (*c)[2]<<endl;
    
    /*
    c+0 = 4
    c+1 = 5
    c+2 = 6
    */
    
    cout << endl;
    
    int *d = b;
    cout << "d + 0 = " << *(d+0) <<endl;
    cout << "d + 1 = " << *(d+1) <<endl;
    cout << "d + 2 = " << *(d+2) <<endl;
    
    //输出
    /*
    c+0 = 4
    c+1 = 5
    c+2 = 6
    */
   
   
   
   
   
    return 0;
}
复制代码

 

 

指针数组和数组指针是 C 和 C++ 中容易混淆的两个概念,下面从定义、语法、用途等方面详细介绍它们的区别。

定义和语法

指针数组

 

指针数组是一个数组,数组中的每个元素都是一个指针。其定义语法为
type *array_name[size];
其中,type 表示指针所指向的数据类型,array_name 是数组的名称,size 是数组的大小。

 

示例代码:
复制代码
#include <stdio.h>

int main() {
    int a = 10, b = 20, c = 30;
    // 定义一个指针数组,每个元素都是 int 类型的指针
    int *ptr_array[3] = {&a, &b, &c}; 

    for (int i = 0; i < 3; i++) {
        printf("Value at ptr_array[%d]: %d\n", i, *ptr_array[i]);
    }
    return 0;
}
复制代码
在上述代码中,ptr_array 是一个包含 3 个 int 类型指针的数组,每个指针分别指向变量 ab 和 c

数组指针

 

数组指针是一个指针,它指向一个数组。其定义语法为:

 

type (*pointer_name)[size];
复制代码
#include <stdio.h>

int main() {
    int arr[3] = {10, 20, 30};
    // 定义一个数组指针,指向一个包含 3 个 int 元素的数组
    int (*ptr)[3] = &arr; 

    for (int i = 0; i < 3; i++) {
        printf("Value at arr[%d]: %d\n", i, (*ptr)[i]);
    }
    return 0;
}
复制代码

在上述代码中,ptr 是一个数组指针,它指向包含 3 个 int 元素的数组 arr

用途

指针数组

 

    • 处理多个字符串:指针数组常用于存储多个字符串的首地址,因为字符串在 C 语言中是以字符数组的形式存储的,每个字符串可以用一个字符指针表示。

复制代码
#include <stdio.h>

int main() {
    // 定义一个指针数组,存储多个字符串的首地址
    char *str_array[] = {"apple", "banana", "cherry"}; 

    for (int i = 0; i < 3; i++) {
        printf("String at str_array[%d]: %s\n", i, str_array[i]);
    }
    return 0;
}
复制代码

数组指针

 

  • 作为函数参数传递二维数组:当需要将二维数组作为参数传递给函数时,使用数组指针可以更方便地处理。
复制代码
#include <stdio.h>

// 函数接受一个数组指针作为参数
void print_2d_array(int (*arr)[3], int rows) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", arr[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
    print_2d_array(arr, 2);
    return 0;
}
复制代码

总结

 

    • 指针数组:本质是数组,数组元素为指针,常用于存储多个指针。
    • 数组指针:本质是指针,指向一个数组,常用于处理多维数组。
 

 

相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示