tab[10][10] in c

Started by
10 comments, last by fir 9 years, 10 months ago

1)

if i got

int tab[10][10];

what type has tab[1] ? is this an 10-int array of

{ tab[1][0], tab[1][1], ... tab[1][9] } ?

2) what is the way or recast one dimensional array into two dimensional, say i got

int tab1[100];

and want to 'recast' it into

int tab2[10][10];


1. tab[1] is the same type as tab[0] - they are both arrays of 10 ints. As arrays of ints, they are also usable as pointers to ints without casting.

2.

int tab1[100];
int (*tab2)[10] = (int (*)[10])tab1;
Note that you have to know the 10 at compile time, which means you'll probably still find yourself using (10*y + x) whenever you need something with a dynamic size (I need dynamic sizing more often than not). I've used this cast when working on arrays full of vectors or triangles or other objects which were just some number of floats each, stored as some type I didn't want to deal with.
Advertisement

1)

if i got

int tab[10][10];

what type has tab[1] ? is this an 10-int array of

{ tab[1][0], tab[1][1], ... tab[1][9] } ?

2) what is the way or recast one dimensional array into two dimensional, say i got

int tab1[100];

and want to 'recast' it into

int tab2[10][10];


1. tab[1] is the same type as tab[0] - they are both arrays of 10 ints. As arrays of ints, they are also usable as pointers to ints without casting.

2.

int tab1[100];
int (*tab2)[10] = (int (*)[10])tab1;
Note that you have to know the 10 at compile time, which means you'll probably still find yourself using (10*y + x) whenever you need something with a dynamic size (I need dynamic sizing more often than not). I've used this cast when working on arrays full of vectors or triangles or other objects which were just some number of floats each, stored as some type I didn't want to deal with.

allright tnx, best answer here (where vortez and nobodynews gave false answers)

could you say me maybe how is read this "(int (*)[10])" type

pointer to array of 10 ints?

sad this is not dymaic working cast, youre right, I would need

it to recast adressing of my pixelbuffer which is dynamic size

so it still will be not working-

- bit sad I can Imagine physically posible in c to doing such

dynamic recasts to though maybe it would break some c common rules where typing is static (with possible recast everywhere but still to exact static type)

tnx for answer

This topic is closed to new replies.

Advertisement