Читаем C++ Primer Plus полностью

To write a function that has a two-dimensional array as an argument, you need to remember that the name of an array is treated as its address, so the corresponding formal parameter is a pointer, just as for one-dimensional arrays. The tricky part is declaring the pointer correctly. Suppose, for example, that you start with this code:

int data[3][4] = {{1,2,3,4}, {9,8,7,6}, {2,4,6,8}};

int total = sum(data, 3);

What should the prototype for sum() look like? And why does the function pass the number of rows (3) as an argument and not also the number of columns (4)?

Well, data is the name of an array with three elements. The first element is, itself, an array of four int values. Thus, the type of data is pointer-to-array-of-four-int, so an appropriate prototype would be this:

int sum(int (*ar2)[4], int size);

The parentheses are needed because the following declaration would declare an array of four pointers-to-int instead of a single pointer-to-array-of-four-int, and a function parameter cannot be an array:

int *ar2[4]

Here’s an alternative format that means exactly the same thing as this first prototype, but, perhaps, is easier to read:

int sum(int ar2[][4], int size);

Either prototype states that ar2 is a pointer, not an array. Also note that the pointer type specifically says it points to an array of four ints. Thus, the pointer type specifies the number of columns, which is why the number of columns is not passed as a separate function argument.

Because the pointer type specifies the number of columns, the sum() function only works with arrays with four columns. But the number of rows is specified by the variable size, so sum() can work with a varying number of rows:

int a[100][4];

int b[6][4];

...

int total1 = sum(a, 100);    // sum all of a

int total2 = sum(b, 6);      // sum all of b

int total3 = sum(a, 10);     // sum first 10 rows of a

int total4 = sum(a+10, 20);  // sum next 20 rows of a

Given that the parameter ar2 is a pointer to an array, how do you use it in the function definition? The simplest way is to use ar2 as if it were the name of a two-dimensional array. Here’s a possible function definition:

int sum(int ar2[][4], int size)

{

    int total = 0;

    for (int r = 0; r < size; r++)

        for (int c = 0; c < 4; c++)

            total += ar2[r][c];

    return total;

}

Again, note that the number of rows is whatever is passed to the size parameter, but the number of columns is fixed at four, both in the parameter declaration for ar2 and in the inner for loop.

Here’s why you can use array notation. Because ar2 points to the first element (element 0) of an array whose elements are array-of-four-int, the expression ar2 + r points to element number r. Therefore ar2[r] is element number r. That element is itself an array-of-four-int, so ar2[r] is the name of that array-of-four-int. Applying a subscript to an array name gives an array element, so ar2[r][c] is an element of the array-of-four-int, hence is a single int value. The pointer ar2 has to be dereferenced twice to get to the data. The simplest way is to use brackets twice, as in ar2[r][c]. But it is possible, if ungainly, to use the * operator twice:

ar2[r][c] == *(*(ar2 + r) + c)  // same thing

To understand this, you can work out the meaning of the subexpressions from the inside out:

ar2              // pointer to first row of an array of 4 int

ar2 + r          // pointer to row r (an array of 4 int)

*(ar2 + r)       // row r (an array of 4 int, hence the name of an array,

                 // thus a pointer to the first int in the row, i.e., ar2[r]

*(ar2 +r) + c    // pointer int number c in row r, i.e., ar2[r] + c

*(*(ar2 + r) + c // value of int number c in row r, i.e. ar2[r][c]

Incidentally, the code for sum() doesn’t use const in declaring the parameter ar2 because that technique is for pointers to fundamental types, and ar2 is a pointer to a pointer.

Functions and C-Style Strings

Recall that a C-style string consists of a series of characters terminated by the null character. Much of what you’ve learned about designing array functions applies to string functions, too. For example, passing a string as an argument means passing an address, and you can use const to protect a string argument from being altered. But there are a few special twists to strings that we’ll unravel now.

Functions with C-Style String Arguments

Suppose you want to pass a string as an argument to a function. You have three choices for representing a string:

• An array of char

• A quoted string constant (also called a string literal)

• A pointer-to-char set to the address of a string

All three choices, however, are type pointer-to-char (more concisely, type char *), so you can use all three as arguments to string-processing functions:

char ghost[15] = "galloping";

char * str = "galumphing";

int n1 = strlen(ghost);          // ghost is &ghost[0]

int n2 = strlen(str);            // pointer to char

int n3 = strlen("gamboling");    // address of string

Перейти на страницу:

Все книги серии Developer's Library

C++ Primer Plus
C++ Primer Plus

C++ Primer Plus is a carefully crafted, complete tutorial on one of the most significant and widely used programming languages today. An accessible and easy-to-use self-study guide, this book is appropriate for both serious students of programming as well as developers already proficient in other languages.The sixth edition of C++ Primer Plus has been updated and expanded to cover the latest developments in C++, including a detailed look at the new C++11 standard.Author and educator Stephen Prata has created an introduction to C++ that is instructive, clear, and insightful. Fundamental programming concepts are explained along with details of the C++ language. Many short, practical examples illustrate just one or two concepts at a time, encouraging readers to master new topics by immediately putting them to use.Review questions and programming exercises at the end of each chapter help readers zero in on the most critical information and digest the most difficult concepts.In C++ Primer Plus, you'll find depth, breadth, and a variety of teaching techniques and tools to enhance your learning:• A new detailed chapter on the changes and additional capabilities introduced in the C++11 standard• Complete, integrated discussion of both basic C language and additional C++ features• Clear guidance about when and why to use a feature• Hands-on learning with concise and simple examples that develop your understanding a concept or two at a time• Hundreds of practical sample programs• Review questions and programming exercises at the end of each chapter to test your understanding• Coverage of generic C++ gives you the greatest possible flexibility• Teaches the ISO standard, including discussions of templates, the Standard Template Library, the string class, exceptions, RTTI, and namespaces

Стивен Прата

Программирование, программы, базы данных

Похожие книги

1С: Бухгалтерия 8 с нуля
1С: Бухгалтерия 8 с нуля

Книга содержит полное описание приемов и методов работы с программой 1С:Бухгалтерия 8. Рассматривается автоматизация всех основных участков бухгалтерии: учет наличных и безналичных денежных средств, основных средств и НМА, прихода и расхода товарно-материальных ценностей, зарплаты, производства. Описано, как вводить исходные данные, заполнять справочники и каталоги, работать с первичными документами, проводить их по учету, формировать разнообразные отчеты, выводить данные на печать, настраивать программу и использовать ее сервисные функции. Каждый урок содержит подробное описание рассматриваемой темы с детальным разбором и иллюстрированием всех этапов.Для широкого круга пользователей.

Алексей Анатольевич Гладкий

Программирование, программы, базы данных / Программное обеспечение / Бухучет и аудит / Финансы и бизнес / Книги по IT / Словари и Энциклопедии
1С: Управление торговлей 8.2
1С: Управление торговлей 8.2

Современные торговые предприятия предлагают своим клиентам широчайший ассортимент товаров, который исчисляется тысячами и десятками тысяч наименований. Причем многие позиции могут реализовываться на разных условиях: предоплата, отсрочка платежи, скидка, наценка, объем партии, и т.д. Клиенты зачастую делятся на категории – VIP-клиент, обычный клиент, постоянный клиент, мелкооптовый клиент, и т.д. Товарные позиции могут комплектоваться и разукомплектовываться, многие товары подлежат обязательной сертификации и гигиеническим исследованиям, некондиционные позиции необходимо списывать, на складах периодически должна проводиться инвентаризация, каждая компания должна иметь свою маркетинговую политику и т.д., вообщем – современное торговое предприятие представляет живой организм, находящийся в постоянном движении.Очевидно, что вся эта кипучая деятельность требует автоматизации. Для решения этой задачи существуют специальные программные средства, и в этой книге мы познакомим вам с самым популярным продуктом, предназначенным для автоматизации деятельности торгового предприятия – «1С Управление торговлей», которое реализовано на новейшей технологической платформе версии 1С 8.2.

Алексей Анатольевич Гладкий

Финансы / Программирование, программы, базы данных