Arrays with arbitrary offset in C

Write here if you have problems with your C source code

Moderator: Board moderators

Post Reply
User avatar
little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Arrays with arbitrary offset in C

Post by little joey » Mon Dec 13, 2004 12:19 pm

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.

Maniac
Experienced poster
Posts: 105
Joined: Tue Oct 14, 2003 3:24 pm
Location: Utrecht, Holland

Post by Maniac » Mon Dec 13, 2004 2:51 pm

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

User avatar
little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post by little joey » Mon Dec 13, 2004 5:21 pm

No, it doesn't make me very happy :)
It costs extra memory and extra processing time...

User avatar
UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post by UFP2161 » Tue Dec 14, 2004 2:23 am

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.

sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post by sumankar » Tue Dec 14, 2004 9:45 am

hi

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

suman.

CDiMa
Experienced poster
Posts: 214
Joined: Fri Oct 17, 2003 5:49 pm
Location: Genova

Post by CDiMa » Tue Dec 14, 2004 12:28 pm

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

User avatar
little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post by little joey » Tue Dec 14, 2004 1:22 pm

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.

sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post by sumankar » Tue Dec 14, 2004 3:34 pm

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.

CDiMa
Experienced poster
Posts: 214
Joined: Fri Oct 17, 2003 5:49 pm
Location: Genova

Post by CDiMa » Tue Dec 14, 2004 4:31 pm

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

Post Reply

Return to “C”