菜鸟教程 -- 学的不仅是技术,更是梦想!

C++ 教程
C++ 教程 C++ 简介 C++ 环境设置 C++ 基本语法 C++ 注释 C++ 数据类型 C++ 变量类型 C++ 变量作用域 C++ 常量 C++ 修饰符类型 C++ 存储类 C++ 运算符 C++ 循环 C++ 判断 C++ 函数 C++ 数字 C++ 数组 C++ 字符串 C++ 指针 C++ 引用 C++ 日期 & 时间 C++ 基本的输入输出 C++ 结构体(struct) C++ vector 容器 C++ 数据结构

C++ 面向对象

C++ 类 & 对象 C++ 继承 C++ 重载运算符和重载函数 C++ 多态 C++ 数据抽象 C++ 数据封装 C++ 接口(抽象类)

C++ 高级教程

C++ 文件和流 C++ 异常处理 C++ 动态内存 C++ 命名空间 C++ 模板 C++ 预处理器 C++ 信号处理 C++ 多线程 C++ Web 编程

C++ 资源库

C++ STL 教程 C++ 导入标准库 C++ 标准库 C++ 有用的资源 C++ 实例 C++ 测验 C++ <iostream> C++ <fstream> C++ <sstream> C++ <iomanip> C++ <array> C++ <vector> C++ <list> C++ <forward_list> C++ <deque> C++ <stack> C++ <queue> C++ <priority_queue> C++ <set> C++ <unordered_set> C++ <map> C++ <unordered_map> C++ <bitset> C++ <algorithm> C++ <iterator> C++ <functional> C++ <numeric> C++ <complex> C++ <valarray> C++ <cmath> C++ <string> C++ <regex> C++ <ctime> C++ <chrono> C++ <thread> C++ <mutex> C++ <condition_variable> C++ <future> C++ <atomic> C++ <type_traits> C++ <typeinfo> C++ <exception> C++ <stdexcept> C++ <cstdio> C++ <cstdint> C++ <memory> C++ <new> C++ <utility> C++ <random> C++ <locale> C++ <codecvt> C++ <cassert> C++ <cwchar> C++ <climits> C++ <cfloat> C++ <cstdlib> C++ <numbers> C++ OpenCV
(追記) (追記ここまで)

C++ 指针数组

C++ 指针 C++ 指针

在我们讲解指针数组的概念之前,先让我们来看一个实例,它用到了一个由 3 个整数组成的数组:

实例

#include<iostream>usingnamespacestd; constintMAX = 3; intmain(){intvar[MAX] = {10, 100, 200}; for(inti = 0; i < MAX; i++){cout << "Value of var[" << i << "] = "; cout << var[i] << endl; }return0; }

当上面的代码被编译和执行时,它会产生下列结果:

Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200

可能有一种情况,我们想要让数组存储指向 int 或 char 或其他数据类型的指针。下面是一个指向整数的指针数组的声明:

int *ptr[MAX];

在这里,把 ptr 声明为一个数组,由 MAX 个整数指针组成。因此,ptr 中的每个元素,都是一个指向 int 值的指针。下面的实例用到了三个整数,它们将存储在一个指针数组中,如下所示:

实例

#include<iostream>usingnamespacestd; constintMAX = 3; intmain(){intvar[MAX] = {10, 100, 200}; int *ptr[MAX]; for(inti = 0; i < MAX; i++){ptr[i] = &var[i]; // 赋值为整数的地址}for(inti = 0; i < MAX; i++){cout << "Value of var[" << i << "] = "; cout << *ptr[i] << endl; }return0; }

当上面的代码被编译和执行时,它会产生下列结果:

Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200

您也可以用一个指向字符的指针数组来存储一个字符串列表,如下:

实例

#include<iostream>usingnamespacestd; constintMAX = 4; intmain(){constchar *names[MAX] = {"Zara Ali", "Hina Ali", "Nuha Ali", "Sara Ali", }; for(inti = 0; i < MAX; i++){cout << "Value of names[" << i << "] = "; cout << names[i] << endl; }return0; }

当上面的代码被编译和执行时,它会产生下列结果:

Value of names[0] = Zara Ali
Value of names[1] = Hina Ali
Value of names[2] = Nuha Ali
Value of names[3] = Sara Ali

C++ 指针 C++ 指针

AI 思考中...

7 篇笔记 写笔记

  1. #0

    1231

    214***[email protected]

    49

    char *names[MAX] 是指针数组, 它的本质是存储指针的数组, 既存储 char 类型的指针的数组, 数组内的每个元素都是一个指针指向一个存储 char 类型的地址:

    #include <iostream>
    using namespace std;
    const int MAX = 4;
    int main(int argc, const char * argv[]) {
     const char *names[MAX] = {
     "Zara Ali",
     "Hina Ali",
     "Nuha Ali",
     "Sara Ali",
     };
     for (int i = 0; i < MAX; i++) {
     cout << " --- names[i] = " << names[i] << endl;
     cout << " --- *names[i] = " << *names[i] << endl;
     cout << endl;
     cout << " --- (*names[i] + 1) = " << (*names[i] + 1) << endl;
     cout << " --- (char)(*names[i] + 1) = " << (char)(*names[i] + 1) << endl;
     cout << " ------------------------------------ " << endl << endl << endl << endl;
     }
     return 0;
    }
    

    1231

    214***[email protected]

    9年前 (2017年04月11日)
  2. #0

    暖暖的太阳

    124***[email protected]

    16

    实例:

    /* -------- Pointer.cpp -------- */
    #include <iostream>
    using namespace std;
    void pointerArray();
    void pointerArray4Char();
    const int MAX = 3;
    int main(void){
     // pointerArray();
     pointerArray4Char();
     return 0;
    }
    void pointerArray(){
     int var[MAX] = {20,30,40};
     int *ptr[MAX];
     for(int i = 0; i < MAX; i++){
     ptr[i] = &var[i];//赋值为整数的地址
     }
     for(int i = 0; i < MAX; i++){
     cout << "Value of var[" << i << "] = ";
     cout << *ptr[i] <<endl;
     }
    }
    /**
     * 用一个指向字符的指针数组来存储一个字符串列表
     * Value of names[0] = sun;
     */
    void pointerArray4Char(){
     const char *names[MAX] = {
     "sun","bin","sunbin"
     };
     for(int i = 0;i < MAX;i++){
     cout <<"Value of names[" << i << "] = ";//输出字符串的值
     cout << names[i] << endl;
     cout <<"Value of *names[" << i << "] = ";//输出指针所指向字符串首地址的值
     cout << *names[i] << endl;
     cout <<"Value of *names[" << i << "]+1 = ";//输出ascii码值
     cout << *names[i]+1 << endl;
     cout <<"Value of *(names[" << i << "]+1) = ";//输出指针所指向字符串首地址上一位的值
     cout << *(names[i]+1) << endl;
     }
    }
    

    cout<<*names[i]<<endl; 输出结果为对应得指针数组的第一个字符,因为 *name 代表指向数组的第一个地址的值。

    暖暖的太阳

    124***[email protected]

    9年前 (2017年06月14日)
  3. #0

    影月5

    132***[email protected]

    206

    C 语言的老司机这里要告诉你的是 C++ 的运算符优先级顺序和C的不一样,有细微差别。

    int *ptr[3]; 

    由于 C++ 运算符的优先级中,* 小于 [],所以 ptr 先和 [] 结合成为数组,然后再和 int * 结合形成数组的元素类型是 int * 类型,得到一个叫一个数组的元素是指针,简称指针数组。

    int *(ptr[3]);

    这个和上面的一样,优先级顺序是 * 小于 (),() 等于 []ptr 先和 [] 结合成为数组,然后再和 int * 结合形成数组的元素类型是 int * 类型,得到一个叫一个数组的元素是指针。

    int (*ptr)[3];

    这个就不一样了,优先级顺序是 * 小于 (),() 等于 [],()[] 的优先级一样,但是结合顺序是从左到右,所以先是 () 里的 *ptr 结合成为一个指针,然后是 (*ptr)[] 相结合成为一个数组,最后叫一个指针 ptr 指向一个数组,简称数组指针。

    影月5

    132***[email protected]

    8年前 (2018年09月19日)
  4. #0

    看树的鸟

    578***[email protected]

    69

    char *names[MAX] 这种字符型的指针数组是存储指针的数组,但是在理解字符型指针数组的时候,可以将它理解为一个二维数组,如 const char *names[4] = {"Zara Ali","Hina Ali","Nuha Ali","Sara Ali",} 可以理解为一个 4 行 8 列的数组,可以用 cout << *(names[i] + j)<< endl 取出数组中的每个元素。

    #include <iostream>
    using namespace std;
    const int MAX = 4;
    int main ()
    {
     const char *names[MAX] = {
     "Zara Ali",
     "Hina Ali",
     "Nuha Ali",
     "Sara Ali",
     };
     for (int i = 0; i < MAX; i++)
     for (int j = 0; j < 8; j++)
     {
     cout << "Value of names[" << i << "] = ";
     cout << *(names[i] + j)<< endl;
     }
     return 0;
    }

    输出结果为:

    Value of names[0] = Z
    Value of names[0] = a
    Value of names[0] = r
    Value of names[0] = a
    Value of names[0] = 
    Value of names[0] = A
    Value of names[0] = l
    Value of names[0] = i
    Value of names[1] = H
    Value of names[1] = i
    Value of names[1] = n
    Value of names[1] = a
    Value of names[1] = 
    Value of names[1] = A
    Value of names[1] = l
    Value of names[1] = i
    Value of names[2] = N
    Value of names[2] = u
    Value of names[2] = h
    Value of names[2] = a
    Value of names[2] = 
    Value of names[2] = A
    Value of names[2] = l
    Value of names[2] = i
    Value of names[3] = S
    Value of names[3] = a
    Value of names[3] = r
    Value of names[3] = a
    Value of names[3] = 
    Value of names[3] = A
    Value of names[3] = l
    Value of names[3] = i

    看树的鸟

    578***[email protected]

    7年前 (2019年02月26日)
  5. #0
    23

    一维动态数组的定义方式:

    type* arrayname = new type [size](); // size可以是变量

    访问与赋值方式:

    for(int i=0;i<size;i++){
     *(arrayname+i)=i; // 或是 arrayname[i];
     cout<<*(arrayname+i)<<"\n";
    }

    释放数组的方式:

    delete [] arrayname;
    6年前 (2020年05月18日)
  6. #0

    leo

    fen***[email protected]

    10

    注意这个定义:

    const char *names[MAX] = {
     "Zara Ali",
     "Hina Ali",
     "Nuha Ali",
     "Sara Ali",
    };
    • char *names[MAX] 含义为:指针数组,即数组的每个元素都是一个指向 char 类型元素的指针;
    • 通过 {...} 中的初始化内容我们可以发现,这个数组的每个元素被赋值了一个字符串,显然这个元素必然为一个指向 char 类型数组的指针;
    • 简单说,char *names[MAX] 中的元素是指向四个 char 类型数组的指针 names[i]。

    leo

    fen***[email protected]

    6年前 (2020年12月03日)
  7. #0

    chenyfan

    101***[email protected]

    22

    指针数组数组指针

    #include <iostream>
    using namespace std;
     
    int main()
    {
     cout << "数组指针--元素为指针变量的一维数组。" << endl;
     int* arr[3]; // 指针数组:元素为指针变量的一维数组。 等价于 int* (arr[3]);
     int p[] = {1,2,3};
     for (int i = 0; i < 3; ++i)
     {
     arr[i] = &p[i];
     }
     cout << arr << endl << endl; //指针数组的地址
     for (int i = 0; i < 3; ++i)
     {
     cout << arr+i << endl;
     cout << *(arr+i) << endl;
     cout << arr[i] << endl;
     cout << *arr[i] << endl;
     }
     cout << endl << endl << "数组指针--指向多维数组地址的指针。" << endl;
     int pp[2][2] = {
     {1,2},
     {3,4}
     }; // p是个二维数组的数组名,相当于一个二级指针常量
     int (*ptr)[2] = pp; // 数组指针:指向多维数组地址的指针
     for (int i = 0; i < 2; ++i)
     {
     cout << ptr + i << endl; // ptr+i 是一维数组p[i]的地址,即ptr+i==&pp[i];
     cout << &pp[i] << endl;
     cout << **(ptr + i)<< endl; // 二级指针 **(ptr + i) = pp[i][0];
     cout << pp[i][0]<< endl; 
     }
     
     cout << *(*(ptr+1)+1) << endl; // 二级指针 *(*(ptr+1)+1) = pp[1][1];
     cout << pp[1][1] << endl;
    }

    chenyfan

    101***[email protected]

    5年前 (2022年01月23日)

点我分享笔记

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址

AltStyle によって変換されたページ (->オリジナル) /