Форум программистов, компьютерный форум CyberForum.ru

Полная явная инициализация многомерного массива - C++

Восстановить пароль Регистрация
 
l2megaboss
0 / 0 / 0
Регистрация: 22.02.2015
Сообщений: 11
11.07.2015, 12:17     Полная явная инициализация многомерного массива #1
Помогите дать определение и приведите пример.
Заранее спасибо!
После регистрации реклама в сообщениях будет скрыта и будут доступны все возможности форума.
rikimaru2013
C++ Game Dev
 Аватар для rikimaru2013
2134 / 967 / 223
Регистрация: 30.11.2013
Сообщений: 3,235
11.07.2015, 12:33     Полная явная инициализация многомерного массива #2
C++
1
2
3
4
5
6
7
8
9
10
11
const int width = 10;
    const int height = 3;
    int** ptr = new int*[height];
 
    for(auto y = 0; y < height; y++)
        ptr[y] = new int[width];
 
    int i = 1;
    for(auto y = 0; y < height; y++)
    for(auto x = 0; x < width; x++)
        ptr[y][x] = i++;
hoggy
5225 / 2116 / 403
Регистрация: 15.11.2014
Сообщений: 4,800
Завершенные тесты: 1
11.07.2015, 13:08     Полная явная инициализация многомерного массива #3
Цитата Сообщение от l2megaboss Посмотреть сообщение
Помогите дать определение и приведите пример.
Заранее спасибо!
определение:

Кликните здесь для просмотра всего текста
8.3.4 Arrays [dcl.array]
1 In a declaration T D where D has the form
D1 [ constant-expressionopt ]
Draft
157 Declarators 8.3 Meaning of declarators
and the type of the identifier in the declaration T D1 is “derived-declarator-type-list T,” then the type of the identifier of
D is an array type. T is called the array element type; this type shall not be a reference type, the (possibly cv-qualified)
type void, a function type or an abstract class type. If the constant-expression (5.19) is present, it shall be an integral
constant expression and its value shall be greater than zero. The constant expression specifies the bound of (number of
elements in) the array. If the value of the constant expression is N, the array has N elements numbered 0 to N-1, and
the type of the identifier of D is “derived-declarator-type-list array of N T.” An object of array type contains a contiguously
allocated non-empty set of N subobjects of type T. If the constant expression is omitted, the type of the identifier of D is
“derived-declarator-type-list array of unknown bound of T,” an incomplete object type. The type “derived-declarator-typelist
array of N T” is a different type from the type “derived-declarator-type-list array of unknown bound of T,” see 3.9. Any
type of the form “cv-qualifier-seq array of N T” is adjusted to “array of N cv-qualifier-seq T,” and similarly for “array of
unknown bound of T.” [ Example:
typedef int A [5] , AA [2][3];
typedef const A CA ; / / type is “array of 5 const int”
typedef const AA CAA ; / / type is “array of 2 array of 3 const int”
— end example ] [Note: an “array of N cv-qualifier-seq T” has cv-qualified type; see 3.9.3. — end note ]
2 An array can be constructed from one of the fundamental types (except void), from a pointer, from a pointer to member,
from a class, from an enumeration type, or from another array.
3 When several “array of” specifications are adjacent, a multidimensional array is created; the constant expressions that
specify the bounds of the arrays can be omitted only for the first member of the sequence. [Note: this elision is useful
for function parameters of array types, and when the array is external and the definition, which allocates storage, is
given elsewhere. — end note ] The first constant-expression can also be omitted when the declarator is followed by an
initializer (8.5). In this case the bound is calculated from the number of initial elements (say, N) supplied (8.5.1), and
the type of the identifier of D is “array of N T.”
4 [ Example:
float fa [17] , * afp [17];
declares an array of float numbers and an array of pointers to float numbers. For another example,
static int x3d [3][5][7];
declares a static three-dimensional array of integers, with rank 3 × 5 × 7. In complete detail, x3d is an array of three
items; each item is an array of five arrays; each of the latter arrays is an array of seven integers. Any of the expressions
x3d, x3d[i], x3d[i][j], x3d[i][j][k] can reasonably appear in an expression. — end example ]
5 [Note: conversions affecting lvalues of array type are described in 4.2. Objects of array types cannot be modified, see
3.10. — end note ]
6 Except where it has been declared for a class (13.5.5), the subscript operator [] is interpreted in such a way that E1[E2]
is identical to *((E1)+(E2)). Because of the conversion rules that apply to +, if E1 is an array and E2 an integer, then
E1[E2] refers to the E2-th member of E1. Therefore, despite its asymmetric appearance, subscripting is a commutative
operation.
7 A consistent rule is followed for multidimensional arrays. If E is an n-dimensional array of rank i × j × ... × k, then
E appearing in an expression is converted to a pointer to an (n − 1)-dimensional array with rank j × ... × k. If the *
Draft
8.3 Meaning of declarators Declarators 158
operator, either explicitly or implicitly as a result of subscripting, is applied to this pointer, the result is the pointed-to
(n−1)-dimensional array, which itself is immediately converted into a pointer.
8 [ Example: consider
int x [3][5];
Here x is a 3 × 5 array of integers. When x appears in an expression, it is converted to a pointer to (the first of three)
five-membered arrays of integers. In the expression x[i] which is equivalent to *(x+i), x is first converted to a pointer
as described; then x+i is converted to the type of x, which involves multiplying i by the length of the object to which
the pointer points, namely five integer objects. The results are added and indirection applied to yield an array (of five
integers), which in turn is converted to a pointer to the first of the integers. If there is another subscript the same argument
applies again; this time the result is an integer. — end example ]
9 [Note: it follows from all this that arrays in C++ are stored row-wise (last subscript varies fastest) and that the first
subscript in the declaration helps determine the amount of storage consumed by an array but plays no other part in
subscript calculations. — end note ]


пример:
C++
1
2
3
4
const char* arr[][3]
{
    "1111", "222", "3333"
};
Цитата Сообщение от rikimaru2013 Посмотреть сообщение
int** ptr = new int*[height];
детонирует непрерывные блоки памяти в куче
и не имеет никакого отношения к массивам языка с++.
l2megaboss
0 / 0 / 0
Регистрация: 22.02.2015
Сообщений: 11
11.07.2015, 13:50  [ТС]     Полная явная инициализация многомерного массива #4
и все, же помогите дать определение(не такое длинное). как сформулировать попроще?)
Yandex
Объявления
11.07.2015, 13:50     Полная явная инициализация многомерного массива
Ответ Создать тему
Опции темы

Текущее время: 04:58. Часовой пояс GMT +3.
КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2016, vBulletin Solutions, Inc.
Рейтинг@Mail.ru