Jump to content
  • Advertisement
Sign in to follow this  
fir

tab[10][10] in c

This topic is 1485 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 [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)

 

 

 

 

 

Share this post


Link to post
Share on other sites
Advertisement

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.

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

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

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

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.

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!