Skip to content

Instantly share code, notes, and snippets.

@zarch
Created April 20, 2012 14:30
Show Gist options
  • Save zarch/2429080 to your computer and use it in GitHub Desktop.
Save zarch/2429080 to your computer and use it in GitHub Desktop.
Cycle for, for a 2d array given the pointer to the first element of the array
#include <math.h>
#include <stdio.h>
typedef struct __move__
{
short int length;
short int *mv;
short int *dir;
double *dist;
} move;
short int mv[8][2] =
{
{ 1, 1}, /* 1 */
{ 1, 0}, /* 2 */
{ 1, -1}, /* 3 */
{ 0, 1}, /* 4 */
{ 0, -1}, /* 6 */
{-1, 1}, /* 7 */
{-1, 0}, /* 8 */
{-1, -1}, /* 9 */
};
short int dir[8] = {1, 2, 3, 4, 6, 7, 8, 9};
double dist[8] = {M_SQRT2, 1, M_SQRT2, 1, 1, M_SQRT2, 1, M_SQRT2};
move movement = {8, mv[0], dir, dist};
int row;
int main()
{
short int row_example[2], j;
for ( row = 0; row < movement.length; row++ )
{
/*
* *( *( mat # i) # j)
* *( mat + i * sizeof( row ) + j * sizeof( T ) )
*/
printf ( "%d) mv0=%d mv1=%d\n",
row,
* ( movement.mv + row * sizeof( row_example )) ,
* ( movement.mv + row * sizeof( row_example ) + sizeof( j )));
}
return 0;
}
int main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment