Skip to content

Instantly share code, notes, and snippets.

@zarch
Created April 19, 2012 08:40
Show Gist options
  • Save zarch/2419722 to your computer and use it in GitHub Desktop.
Save zarch/2419722 to your computer and use it in GitHub Desktop.
Arrays and pointers in C
#include <stdio.h>
#include <stdlib.h>
#define NROWS 8
#define NCOLS 3
/*
*
* This sample code is taken from:
*
* http://www.ibiblio.org/pub/languages/fortran/append-c.html
*
*
*/
int func1();
int func2();
int func3();
int func4();
int func5();
int
main()
{
short mat[NROWS][NCOLS],i,j;
for ( i = 0; i < NROWS; i++ )
for ( j = 0; j < NCOLS; j++ )
{
mat[i][j] = i*10 + j;
}
printf ( " Initialized data to: " );
for ( i = 0; i < NROWS; i++ )
{
printf ( "\n" );
for ( j = 0; j < NCOLS; j++ )
{
printf ( "%5.2d", mat[i][j] );
}
}
printf ( "\n" );
func1 ( mat );
func2 ( mat );
func3 ( mat );
func4 ( mat );
func5 ( mat );
return 0;
}
/*
Method #1 (No tricks, just an array with empty first dimension)
===============================================================
You don't have to specify the first dimension!
*/
int func1 ( short mat[][NCOLS] )
{
register short i, j;
printf ( "Func1: Declare as matrix, explicitly specify second dimension: " );
for ( i = 0; i < NROWS; i++ )
{
printf ( "\n" );
for ( j = 0; j < NCOLS; j++ )
{
printf ( "%5.2d", mat[i][j] );
}
}
printf ( "\n" );
return 0;
}
/*
Method #2 (pointer to array, second dimension is explicitly specified)
======================================================================
*/
int func2 ( short ( *mat ) [NCOLS] )
{
register short i, j;
printf ( "Func2: Declare as pointer to column, explicitly specify 2nd dim: " );
for ( i = 0; i < NROWS; i++ )
{
printf ( "\n" );
for ( j = 0; j < NCOLS; j++ )
{
printf ( "%5.2d", mat[i][j] );
}
}
printf ( "\n" );
return 0;
}
/*
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 ( "Func3: Declare as single-pointer, manual offset computation: " );
for ( i = 0; i < NROWS; i++ )
{
printf ( "\n" );
for ( j = 0; j < NCOLS; j++ )
{
printf ( "%5.2d", * ( mat + NCOLS*i + j ) );
}
}
printf ( "\n" );
return 0;
}
/*
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[NROWS];
for ( i = 0; i < NROWS; i++ )
index[i] = ( short * ) mat + NCOLS*i;
printf ( "Func4: Declare as double-pointer, use auxiliary pointer array: " );
for ( i = 0; i < NROWS; i++ )
{
printf ( "\n" );
for ( j = 0; j < NCOLS; j++ )
{
printf ( "%5.2d", index[i][j] );
}
}
printf ( "\n" );
return 0;
}
/*
Method #5 (single pointer, using an auxiliary array of pointers)
================================================================
*/
int func5 ( short *mat[NCOLS] )
{
short i, j, *index[NROWS];
for ( i = 0; i < NROWS; i++ )
index[i] = ( short * ) mat + NCOLS*i;
printf ( "Func5: Declare as single-pointer, use auxiliary pointer array: " );
for ( i = 0; i < NROWS; i++ )
{
printf ( "\n" );
for ( j = 0; j < NCOLS; j++ )
{
printf ( "%5.2d", index[i][j] );
}
}
printf ( "\n" );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment