您的位置:首页 > 其它

一本介绍C指针的书--字符串和字符串数组6.3

2012-08-04 00:00 393 查看
The following program illustrates this using integer arrays instead of character arrays.

------------------- program 6.1 ----------------------

/* Program 6.1 from PTRTUT10.HTM 6/13/97*/

#include <stdio.h>

#define ROWS 5

#define COLS 10

int multi[ROWS][COLS];

int main(void)

{

int row, col;

for (row = 0; row < ROWS; row++)

{

for (col = 0; col < COLS; col++)

{

multi[row][col] = row*col;

}

}

for (row = 0; row < ROWS; row++)

{

for (col = 0; col < COLS; col++)

{

printf("\n%d ",multi[row][col]);

printf("%d ",*(*(multi + row) + col));

}

}

return 0;

}

----------------- end of program 6.1 ---------------------

Because of the double de-referencing required in the pointer version, the name of a 2

dimensional array is often said to be equivalent to a pointer to a pointer. With a three

dimensional array we would be dealing with an array of arrays of arrays and some might

say its name would be equivalent to a pointer to a pointer to a pointer. However, here we

have initially set aside the block of memory for the array by defining it using array

notation. Hence, we are dealing with a constant, not a variable. That is we are talking

about a fixed address not a variable pointer. The dereferencing function used above

permits us to access any element in the array of arrays without the need of changing the

value of that address (the address of multi[0][0] as given by the symbol multi).
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  指针