📄 82549-arrays and pointers in c.txt
字号:
type mat[m][n], *ptr1, **ptr2;
ptr2 = &ptr1;
ptr1 = (type *)mat;
but that wouldn't work either, the information on the array "width" (n),
is lost, and we would get right only the first row, then we will have
again wild memory accesses.
A possible way to make a double pointer work with a 2D array notation
is having an auxiliary array of pointers, each of them points to a
row of the original matrix.
type mat[m][n], *aux[m], **ptr2;
ptr2 = (type **)aux;
for (i = 0 ; i < m ; i++)
aux[i] = (type *)mat + i * n;
Of course the auxiliary array could be dynamic.
An example program:
#include <stdio.h>
#include <stdlib.h>
main()
{
long mat[5][5], **ptr;
mat[0][0] = 3;
ptr = (long **)mat;
printf(" mat %p \n", mat);
printf(" ptr %p \n", ptr);
printf(" mat[0][0] %d \n", mat[0][0]);
printf(" &mat[0][0] %p \n", &mat[0][0]);
printf(" &ptr[0][0] %p \n", &ptr[0][0]);
return;
}
The output on VAX/VMS is:
mat 7FDF6310
ptr 7FDF6310
mat[0][0] 3
&mat[0][0] 7FDF6310
&ptr[0][0] 3
We can see that "mat[0][0]" and "ptr[0][0]" are different objects
(they have different addresses), although "mat" and "ptr" have
the same value.
What methods for passing a 2D array to a subroutine are allowed?
----------------------------------------------------------------
Following are 5 alternative ways to handle in C an array passed from
a Fortran procedure or another c routine.
Various ways to declare and use such an array are presented by examples
with an array made of 3x3 shorts (INTEGER*2). All 5 methods work on
a VAX/VMS machine with DECC.
#include <stdio.h>
#include <stdlib.h>
int func1();
int func2();
int func3();
int func4();
int func5();
main()
{
short mat[3][3],i,j;
for(i = 0 ; i < 3 ; i++)
for(j = 0 ; j < 3 ; j++)
{
mat[i][j] = i*10 + j;
}
printf(" Initialized data to: ");
for(i = 0 ; i < 3 ; i++)
{
printf("\n");
for(j = 0 ; j < 3 ; j++)
{
printf("%5.2d", mat[i][j]);
}
}
printf("\n");
func1(mat);
func2(mat);
func3(mat);
func4(mat);
func5(mat);
}
/*
Method #1 (No tricks, just an array with empty first dimension)
===============================================================
You don't have to specify the first dimension!
*/
int func1(short mat[][3])
{
register short i, j;
printf(" Declare as matrix, explicitly specify second dimension: ");
for(i = 0 ; i < 3 ; i++)
{
printf("\n");
for(j = 0 ; j < 3 ; j++)
{
printf("%5.2d", mat[i][j]);
}
}
printf("\n");
return;
}
/*
Method #2 (pointer to array, second dimension is explicitly specified)
======================================================================
*/
int func2(short (*mat)[3])
{
register short i, j;
printf(" Declare as pointer to column, explicitly specify 2nd dim: ");
for(i = 0 ; i < 3 ; i++)
{
printf("\n");
for(j = 0 ; j < 3 ; j++)
{
printf("%5.2d", mat[i][j]);
}
}
printf("\n");
return;
}
/*
Method #3 (Using a single pointer, the array is "flattened")
============================================================
With this method you can create general-purpose routines.
The dimensions doesn't appear in any declaration, so you
can add them to the formal argument list.
The manual array indexing will probably slow down execution.
*/
int func3(short *mat)
{
register short i, j;
printf(" Declare as single-pointer, manual offset computation: ");
for(i = 0 ; i < 3 ; i++)
{
printf("\n");
for(j = 0 ; j < 3 ; j++)
{
printf("%5.2d", *(mat + 3*i + j));
}
}
printf("\n");
return;
}
/*
Method #4 (double pointer, using an auxiliary array of pointers)
================================================================
With this method you can create general-purpose routines,
if you allocate "index" at run-time.
Add the dimensions to the formal argument list.
*/
int func4(short **mat)
{
short i, j, *index[3];
for (i = 0 ; i < 3 ; i++)
index[i] = (short *)mat + 3*i;
printf(" Declare as double-pointer, use auxiliary pointer array: ");
for(i = 0 ; i < 3 ; i++)
{
printf("\n");
for(j = 0 ; j < 3 ; j++)
{
printf("%5.2d", index[i][j]);
}
}
printf("\n");
return;
}
/*
Method #5 (single pointer, using an auxiliary array of pointers)
================================================================
*/
int func5(short *mat[3])
{
short i, j, *index[3];
for (i = 0 ; i < 3 ; i++)
index[i] = (short *)mat + 3*i;
printf(" Declare as single-pointer, use auxiliary pointer array: ");
for(i = 0 ; i < 3 ; i++)
{
printf("\n");
for(j = 0 ; j < 3 ; j++)
{
printf("%5.2d", index[i][j]);
}
}
printf("\n");
return;
}
Return to contents page
</html
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -