Skip to content

Array

本章讨论array,重要参考如下两篇文章:

1) stackoverflow How do I use arrays in C++?

2) cppreference Array declaration

Pointer and array

Array to pointer decay

一维array会decay to 一级pointer,显然两者能够进行对应;static array vs dynamic array,其实dynamic array就是一个pointer;

因为array和memory的结构的相同:都是sequence,这就导致了两者的类似;

array和pointer有着相同的arithmetic;

上述两个相似点,导致了pointer和array的很多地方的相似;

Multidimensional array和multiple-level pointer有着对应关系的;可以使用一个multiple-level pointer来访问一个multidimensional array,典型的例子:

1 char **argvchar* argv[]

Multidimensional array和multiple-level pointer都可以使用containing关系来进行理解;

n-dimensional array、array of pointer、multiple-degree and n-dimensional array

https://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c?noredirect=1&lq=1

Multidimensional arrays

参见 Multidimensional-array 章节。

With arrays, why is it the case that a[5] == 5[a]?

As Joel points out in Stack Overflow podcast #34, in C Programming Language (aka: K & R), there is mention of this property of arrays in C: a[5] == 5[a]

Joel says that it's because of pointer arithmetic but I still don't understand. Why does a[5] == 5[a]?

A

The C standard defines the [] operator as follows:

a[b] == *(a + b)

Therefore a[5] will evaluate to:

*(a + 5)

and 5[a] will evaluate to:

*(5 + a)

a is a pointer to the first element of the array. a[5] is the value that's 5 elements further from a, which is the same as *(a + 5), and from elementary school math we know those are equal (addition is commutative).

A

#include <iostream>

int main(int argc, char** argv)
{
    std::cout << std::boolalpha << std::endl;
    bool Equal = "ABCD"[2] == 2["ABCD"];
    std::cout << Equal << std::endl;
    Equal = 2["ABCD"] == 'C';
    std::cout << Equal << std::endl;
    Equal = "ABCD"[2] == 'C';
    std::cout << Equal << std::endl;
}

Array class

1) Dynamic array: std::vector<T> since C++98

2) Static array: std::array since C++11

TODO

2 Dimensional Char Array C++

Two-Dimensional Char Array

valarray

Promoting a raw pointer to valarray