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.
Arrays with arbitrary offset in C
Moderator: Board moderators
-
- Guru
- Posts: 1080
- Joined: Thu Dec 19, 2002 7:37 pm
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.
[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.
-
- Guru
- Posts: 1080
- Joined: Thu Dec 19, 2002 7:37 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.
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.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.
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

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