Page 1 of 1

Arrays with arbitrary offset in C

Posted: Mon Dec 13, 2004 12:19 pm
by little joey
It's been almost a year since I switched from Pascal to C. Both languages have their strong and weak areas, and I refrain from giving an opinion on which is the better of the two. Code written in C is on average faster then the same code in Pascal for the problems here on UVA, but I don't think C is faster than Pascal in general.
There are, however, some language features from Pascal that I keep missing in C. The WITH ... DO ... construction, local functions and arrays with arbitrary offset are the features I miss most. On this last issue I need your help.

In Pascal you can declare a one dimensional array with any (integer) range, f.i.[pascal]myarray: array[-30..45] of integer;[/pascal] after which you can use all array elements from myarray[-30] upto myarray[45]. It's not so difficult to do the same in C with the help of a pointer:[c]int dummy[76];
int *myarray=&dummy[30];[/c]and now you can also access all elements of dummy[] using myarray[-30] upto myarray[45].

But for two (and higher) dimensions the situation is not clear to me. How should I declare the C equivalent of[pascal]myarray2: array[-20..20,-20..20] of integer;[/pascal]I want to access all elements of dummy2[][] with expressions like myarray2[-7][3], myarray2[9][0], etc. I have the feeling that it should be possible, but I don't know how. The simplest looking way[c]int dummy2[41][41];
int *myarray2[][41]=&dummy2[20][20];[/c]doesn't work.

Posted: Mon Dec 13, 2004 2:51 pm
by Maniac
How about this (I haven't tried it myself):
[c]int dummy2[41][41];
int *mid[41];
for(int i=0; i<41; i++) mid = &dummy2[20];
int **myarray2 = &mid[20];
[/c]
Although I don't think this will make you very happy for a 6D-array :-)
Erik

Posted: Mon Dec 13, 2004 5:21 pm
by little joey
No, it doesn't make me very happy :)
It costs extra memory and extra processing time...

Posted: Tue Dec 14, 2004 2:23 am
by UFP2161
You can always #define a macro to do it..
[c]#define myarray(a,b) realarray[a+20][b+20];[/c]
Of course, it would be less efficient since it's doing a+20 and then another addition on the pointer itself, and you'd hafta write code like myarray(-1, -2) instead of myarray[-1][-2].

At least you get rid of the extra memory now at the cost of inefficiency and readability =P

And you might run into some problems if a and b are some complex expressions.

Posted: Tue Dec 14, 2004 9:45 am
by sumankar
hi

you can go over to comp.lang.c for all your troubles

suman.

Posted: Tue Dec 14, 2004 12:28 pm
by CDiMa
sumankar wrote:hi

you can go over to comp.lang.c for all your troubles

suman.
An alternative is to post a question in this forum 8)
[c]
#define NROWS 41
#define NCOLS 41

int dummy[NROWS][NCOLS];
int (*myarray)[NCOLS] = (int (*)[NCOLS]) &(dummy[(NROWS-1)/2][(NCOLS-1)/2]);
[/c]

Ciao!!!

Claudio

Posted: Tue Dec 14, 2004 1:22 pm
by little joey
Thanks Claudio!
That was exactly what I needed. It's easily extended to more than two dimensions, now I see the light 8)

I think my old brain has become too stiff to ever learn to work with the 'inside-out-edness' of declarations and casts, but once I saw the solution, it became logical.

Posted: Tue Dec 14, 2004 3:34 pm
by sumankar
Claudio,

i knew this was coming.Well I was thinking on programming in general,
and the laxity which we allow in it (please do not read in between the lines),
the language standard, what it says, and the fact that we already have a lot
of that discussed out there - so a time saver in some way.

Anyways, if you want I can remove that **really really** offensive post.

Regards,
Suman.

Posted: Tue Dec 14, 2004 4:31 pm
by CDiMa
sumankar wrote:Claudio,

i knew this was coming.Well I was thinking on programming in general,
and the laxity which we allow in it (please do not read in between the lines),
the language standard, what it says, and the fact that we already have a lot
of that discussed out there - so a time saver in some way.

Anyways, if you want I can remove that **really really** offensive post.

Regards,
Suman.
Don't worry, I was only joking and wasn't offended at all by your post.
I was only nitpicking on the fact that this is supposed to be exactly a forum for help in C language and abused the fact that I knew the answer :P

Indeed comp.lang.c and their faq is a big resource to rely on and surely I recommend it!

Ciao!!!

Claudio