• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
fir

tab[10][10] in c

11 posts in this topic

This is deadly confusing is such array

 

int tab[10][10];

 

a lite/solid block of 100 ints (equivalent of tab[a+b*10] where a is index of right [] and b index of left [] I mean [b][a]) or this is really (as someone say

but it is really hard to belive for me, an array of 10 pointers to 10-int chunks)?

(so really its size is 100 ints + 10 pointers and this pointers can lay far from int contents?)

 

could comeone finaly explain it to me (but please tell me a sure/solid answer youre sure of, TNX)

 

 

 

 

 

0

Share this post


Link to post
Share on other sites

What you seem to think it is is called a ragged array (ragged because the rows can be different lengths) and, in C and C++, you'd have to create this manually

int *tab[10];
for(int i = 0; i < 10; ++i)
{
    tab[i] = new int[10];
}

There is no built-in support for ragged arrays in the way you are (I think) describing.

1

Share this post


Link to post
Share on other sites

You can access the first item in the array as tab[0][0], or as tab[0], or as *tab. The compiler treats all of these the same.


I don't consider those are all the same. tab[0][0] is an int, and tab[0] and *tab are arrays. The things they refer to all have the same memory address, but the compiler does not treat them all the same. Well, the last two are the same because *x and x[0] are generally the same (I think operator overloading is about the only thing that can make them work differently).

int tab[10][10];

int* p1 = tab[0];
int* p2 = (int*)tab;
int* p3 = &tab[0][0];
int (*p4)[10] = tab;

assert(p1 == p2 && p1 == p3 && p1 == *p4);
2

Share this post


Link to post
Share on other sites

 

You can access the first item in the array as tab[0][0], or as tab[0], or as *tab. The compiler treats all of these the same.


I don't consider those are all the same. tab[0][0] is an int, and tab[0] and *tab are arrays. The things they refer to all have the same memory address, but the compiler does not treat them all the same. Well, the last two are the same because *x and x[0] are generally the same (I think operator overloading is about the only thing that can make them work differently).

int tab[10][10];

int* p1 = tab[0];
int* p2 = (int*)tab;
int* p3 = &tab[0][0];
int (*p4)[10] = tab;

assert(p1 == p2 && p1 == p3 && p1 == *p4);

 

Allright thanx for the answers all (Im not quite that newbie in c but still there are some subtle things that can confuse me)

Got yet two questons related to thing said above

 

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];

 

I would preferably got to pointers  tab1 for flat adressing and tab2 for [][] adressing of the same memory chunk)

 

(could then acces like tab[y][x]; could be slower or faster  than hand writting of tab[y*10+x] ?)

0

Share this post


Link to post
Share on other sites

Just think about the [] as a way for the compiler to calculate the offset from the first element of the array to the one you want.

 

1)

 


int tab[10][10];

 

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

 

This make no sense, it won't even compile. tab[1][0] is just a way of saying, hey, i want the int at location &tab[0][0] + (((1 * 10) + 0) * sizeof(int))

 

2) no need for indexing at all...

 

memcpy(&tab1[0], &tab2[0][0], 100 * sizeof(int));

 

To answer the question, i doubt it will be faster, since the compiler does the exact same thing i believe.

Edited by Vortez
0

Share this post


Link to post
Share on other sites

For your final comment about speed..

 

a. Let the compiler do as much as possible, it usually knows best.

b. You typically shouldn't care about performance for small things like these. Both are way faster than "enough" and trying to optimize will typically just give you headaches for other reasons.

c. There is rarely a universal answer to which is faster in these types of questions, it'll depend on lots of things and will therefore vary on a case by case basis.

0

Share this post


Link to post
Share on other sites


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] } ?

I believe Vortez meant specifically the notation you used at the bottom ( { tab[1][0], ... etc ) as 'not making sense'. I would say that tab[1] does have a type, which is an array of 10 ints. That is, if you did size(tab[1]) that would return the same result as 10*sizeof(int). This doesn't really help anything though because you can't do much with this information. You can't assign arrays to each other.

 


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];

I would preferably got to pointers tab1 for flat adressing and tab2 for [][] adressing of the same memory chunk)

(could then acces like tab[y][x]; could be slower or faster than hand writting of tab[y*10+x] ?)
You can't cast arrays of one type to another type like that in C.

 

Besides, you generally don't want to waste your time optimizing at that level. What you should be spending time on is understanding things at the algorithm level. Doesn't matter how quick it is to access tab[y][x] vs tab[y*10+x] if the algorithm you're using it in stinks. Even if you picked the best algorithms the best optimization is probably to use the appropriate compiler intrinsic. The best way to tell what to focus on and when is to understand how to profile your code. What works best in one situation won't work best in another situation.

0

Share this post


Link to post
Share on other sites

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.
1

Share this post


Link to post
Share on other sites

 

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

-2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0