一本介绍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).
------------------- 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).
相关文章推荐
- 一本介绍C指针的书--字符串和字符串数组6.2
- 一本介绍C指针的书--字符串和字符串数组6.1
- 一本介绍C指针的书--字符串4.1
- 一本介绍C指针的书--指针和字符串3.2
- 一本介绍C指针的书--指针和字符串3.1
- 一本介绍C指针的书--指针的类型及数组2.2
- 一本介绍C指针的书--指针的类型及数组2.1
- C语言字符串指针---与数组0811
- 三十二、数组、指针和字符串:动态内存分配和释放
- 字符串指针 与 字符数组
- 第四章 数组,字符串和指针
- 从C++指针定义字符串和数组定义字符串区别到C++堆和栈区别
- C/C++字符串,字符数组,字符指针及其相互静态拷贝与追加的安全问题解决方案(1)
- 鸡啄米:C++编程入门系列之二十八(数组、指针和字符串:指针的赋值和指针运算)
- 字符串指针与字符串数组的区别
- 「C」 数组、字符串、指针
- 字符串数组与指针
- 三十二、数组、指针和字符串:string类
- 指针定义字符串和数组定义字符串的区别
- 实现字符串循环右移n 位与左移n位(不建立数组,直接用指针)