473,394 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Pascal -> C

Hallo allemaal,
In my Pascal program I gathered all messages in one function. All
messages have their own number. One of the advantages of this method
is that if someone wants to change a messages, he only has to look in
one place.

The function is declared as follow:
function zin(by : byte) : string;

A line calling the function can look like as simple as
this:
writeln(zin(12));

So far I have found a replacement for this function but I'm not
satisfied with it. I defined a 'char pst[255]' and the function 'void
zin(int by)' fills pst with the wanted message. But that means I now
need two lines instead of one:
zin(12);
printf("%s", pst);

Of course I tried to turn 'void zin(int by)' into a funtion that
returns a value so I could combine those two line to something like
'printf("%s", zin(12));' but I either got an compilation error or only
nonsense appeared on the screen.

In the meanwhile I found a solution in the form of
char *zin[20] = { "..", ""..." etc. }
printf("%s", zin[12]);

But that works fine for fixed messages but what about messages that
are generated by a function on the fly?

Any help is welcome!
--
___
/ __|__
/ / |_/ Groetjes, Ruud Baltissen
\ \__|_\
\___| http://Ruud.C64.org
Oct 21 '08 #1
18 2109
Ruud said:

<snip>
Of course I tried to turn 'void zin(int by)' into a funtion that
returns a value so I could combine those two line to something like
'printf("%s", zin(12));' but I either got an compilation error or only
nonsense appeared on the screen.
Why not modify zin to return (a pointer to the first character of) the
string?

const char *zin(int msg)
{
static char pst[MAX_LENGTH]; /* define MAX_LENGTH at your convenience */

const char *msglist[] =
{
"Hello",
"Goodbye",
"Whatever"
};
size_t count = sizeof msglist / sizeof msglist[0];
if(msg >= 0 && msg < count)
{
strcpy(pst, msglist[msg]);
}
else
{
/* presumably this is where your machine-generated messages are
generated, and loaded into pst
*/
}

return pst;
}

Now printf("%s", zin(12)) becomes legal - but beware! The static buffer
means that you've only got one storage area for "the current message", so
printf("%s %s", zin(12), zin(1)) will very likely not do what you want it
to do. If that's a problem, you need to think about passing a buffer into
the function.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 21 '08 #2
On 21 Oct, 09:38, Ruud <Ruud.Baltis...@apg.nlwrote:
In my Pascal program I gathered all messages in one function. All
messages have their own number. One of the advantages of this method
is that if someone wants to change a messages, he only has to look in
one place.
and if you want to know what a message is it's never in the
place where you look...

Very long macro names can help.

error (27);

and somewhere is
{27, "communications link is lost"}

hence a better macro might be
error (LINK_LOST);

I worked on a project where the error macros looked like this
error (SYSTEM_ERROR_COMMUNICATIONS_LINK_IS_LOST);

But this was internationalising (I18Ning) the actual output
strings.
The function is declared as follow:
function zin(by : byte) : string;

A line calling the function can look like as simple as
this:
writeln(zin(12));

So far I have found a replacement for this function but I'm not
satisfied with it. I defined a 'char pst[255]' and the function 'void
zin(int by)' fills pst with the wanted message. But that means I now
need two lines instead of one:
zin(12);
printf("%s", pst);
you could return static strings

const char* zin (int n)
{
switch (n)
{
case 1: return "link lost";
break;
case 2: return "O2 over pressure";
break;
default:
return "bad error code";
}
}

printf ("%s", zin(12));
Of course I tried to turn 'void zin(int by)' into a funtion that
returns a value so I could combine those two line to something like
'printf("%s", zin(12));' but I either got an compilation error or only
nonsense appeared on the screen.
show your code

In the meanwhile I found a solution in the form of
char *zin[20] = { "..", ""..." etc. }
printf("%s", zin[12]);
yep. Or

struct String_tab_entry
{
int n;
const char *str;
};

String_tab_entry string_tab [] =
{
{12, "tile lost"},
{99, "intruder"},
(101, "fire"},
};

Then search the array. The strings then don't have to be contiguous
or even ordered. (tho' ordered can make the search faster)
But that works fine for fixed messages but what about messages that
are generated by a function on the fly?
In that case you need a buffer of some sort. This is fairly
standard C problem. Solutions are

1. use a static buffer

const char* zin (int n, const char* arg)
{
static char buffer [256];

switch (n)
{
case 1:
sprintf (buffer, "link lost %s", arg);
return buffer;
}
}

Multiple calls may be a problem

printf ("%s %s", zin(12), zin(13));

2. pass a buffer

const char* zin (int n, char *buffer, const char* arg)
{ switch (n)
{
case 1:
sprintf (buffer, "link lost %s", arg);
return buffer;
}
}

3. malloc a buffer (but then someone has to clean up
afterwards).

4. Make zin() a lot cleverer (Untested code!)

#include <stdarg.h>

typedef void PrintFunc (const char*);

void zin (int n, PrintFunc *print, const char *format, ...)
{
va_list args;
va_start (args, format);
char buffer [256];
vsprintf (buffer, format, args);
print (buffer);
}

void print (const char *s)
{
printf ("%s", s);
}

zin (23, print, "link lost %d", link_id);
This is a bit untidy. But the point is you can pass arbitary
arguments to zin() using stdarg functionality. You can
also pass function arguments to do the actual i/o.

Without knowing more about your app I can't go any furthur.
Hope I've given you some ideas.
--
Nick Keighley


Oct 21 '08 #3
Nick Keighley <ni******************@hotmail.comwrote:
On 21 Oct, 09:38, Ruud <Ruud.Baltis...@apg.nlwrote:
In my Pascal program I gathered all messages in one function. All
messages have their own number. One of the advantages of this method
is that if someone wants to change a messages, he only has to look in
one place.

and if you want to know what a message is it's never in the
place where you look...

Very long macro names can help.

error (27);

and somewhere is
{27, "communications link is lost"}

hence a better macro might be
error (LINK_LOST);

I worked on a project where the error macros looked like this
error (SYSTEM_ERROR_COMMUNICATIONS_LINK_IS_LOST);
Or in this case, even better, use an enum.

Richard
Oct 21 '08 #4

I was not going to post this but now the issue has come up twice it
seems worth it...

Nick Keighley <ni******************@hotmail.comwrites:
On 21 Oct, 09:38, Ruud <Ruud.Baltis...@apg.nlwrote:
<snip>
>But that works fine for fixed messages but what about messages that
are generated by a function on the fly?

In that case you need a buffer of some sort. This is fairly
standard C problem. Solutions are

1. use a static buffer

const char* zin (int n, const char* arg)
{
static char buffer [256];

switch (n)
{
case 1:
sprintf (buffer, "link lost %s", arg);
return buffer;
}
}

Multiple calls may be a problem

printf ("%s %s", zin(12), zin(13));
If you are stuck with this interface and need the above to work you
can, of course, allow a fixed number of "multiple" calls buy writing:

#define N_BUFFERS 3

const char* zin(int n, const char* arg)
{
static char buffers[N_BUFFERS][256];
static int nb = 0;
char *buffer = buffers[nb++];
nb %= N_BUFFERS;

/* code as before... */
}

but this has a relatively high "yuck" coefficient.

--
Ben.
Oct 21 '08 #5
On 21 Oct, 15:32, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
Nick Keighley <nick_keighley_nos...@hotmail.comwrote:
On 21 Oct, 09:38, Ruud <Ruud.Baltis...@apg.nlwrote:
In my Pascal program I gathered all messages in one function. All
messages have their own number. One of the advantages of this method
is that if someone wants to change a messages, he only has to look in
one place.
and if you want to know what a message is it's never in the
place where you look...
Very long macro names can help.
* *error (27);
and somewhere is
* * {27, "communications link is lost"}
hence a better macro might be
* * error (LINK_LOST);
I worked on a project where the error macros looked like this
* * error (SYSTEM_ERROR_COMMUNICATIONS_LINK_IS_LOST);

Or in this case, even better, use an enum.
why is an enum "better"?

--
Nick Keighley
Oct 22 '08 #6
On 21 Oct, 15:33, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
I was not going to post this but now the issue has come up twice it
seems worth it...



Nick Keighley <nick_keighley_nos...@hotmail.comwrites:
On 21 Oct, 09:38, Ruud <Ruud.Baltis...@apg.nlwrote:
<snip>
But that works fine for fixed messages but what about messages that
are generated by a function on the fly?
In that case you need a buffer of some sort. This is fairly
standard C problem. Solutions are
* *1. use a static buffer
* *const char* zin (int n, const char* arg)
* *{
* * * *static char buffer [256];
* * * *switch (n)
* * * *{
* * * * * *case 1:
* * * * * * * * sprintf (buffer, "link lost %s", arg);
* * * * * * * * return buffer;
* * * *}
* *}
* *Multiple calls may be a problem
* * * *printf ("%s %s", zin(12), zin(13));

If you are stuck with this interface and need the above to work you
can, of course, allow a fixed number of "multiple" calls buy writing:

* * #define N_BUFFERS 3

* * const char* zin(int n, const char* arg)
* * {
* * * * static char buffers[N_BUFFERS][256];
* * * * static int nb = 0;
* * * * char *buffer = buffers[nb++];
* * * * nb %= N_BUFFERS;

* * * * /* code as before... */
* * }

but this has a relatively high "yuck" coefficient.
too true!

I tend to live with the static buffer. Live with
fact I *may* overflow it. And never have statements
with multiple uses of "zin()".

--
Nick Keighley

Oct 22 '08 #7
Hallo allemaal,
@Richard:
you need to think about passing a buffer into the function.
That is one of the things I tried that did work. But passing a buffer
appeared a bit clumsy to me.
As I already mentioned, my original funtion 'zin' (FYI: Dutch for
'sentence') contains some concatinations with other variables which I
like to keep. So I think I have no choice then using this solution.
@Nick:
show your code
char pst[255];

main(int argc, char *argv[])
{
int by;

printf("\n\n\n");

for (by = 0; by < 2; by++)
printf("%s\n", szin(by));

return(0);
}
char szin(int w)
{
char *c;

switch(w)
{
case 0 : strcpy(pst, "Unknown/incorrect placed Directive");
break;
case 1 : strcpy(pst, "Line ended incorrectly");
break;
default : strcpy(pst, "?????????");
}

c = pst;
return(*c);
}
The above compiles fine but produces the nonsence. I'm quite sure it
has to do with 'c = pst' but what is wrong, I have no idea. 'pst' on
itself is a pointer (AFAIK). I can return '*c' but returning 'pst'
causes an error.
After writing the above I found a piece of code that explained some
things.

c = pst;

strcpy(pst, "abcdefgh");
c = pst;
printf("-1- %s\n", pst);
printf("-2- %c\n", *c);
printf("-3- %s\n", *c);
c = &pst[1];
printf("-4- %c\n", *c);
printf("-5- %s\n", *c);

return(*c);

Line -1- outputted 'abcdefgh', -2- 'a' and -4- 'b'. -3- and -5-
produce nonsence. Still the question: why can't I return 'pst'?
@Ben & Nick:

Thanks for the info but, being a beginner, these are things I don't
understand completely yet.
--
* * ___
* */ __|__
* / / *|_/ * * Groetjes, Ruud Baltissen
* \ \__|_\
* *\___| * * *http://Ruud.C64.org

Oct 22 '08 #8
Ruud said:
Hallo allemaal,
@Richard:
>you need to think about passing a buffer into the function.

That is one of the things I tried that did work. But passing a buffer
appeared a bit clumsy to me.
As I already mentioned, my original funtion 'zin' (FYI: Dutch for
'sentence') contains some concatinations with other variables which I
like to keep. So I think I have no choice then using this solution.
@Nick:
>show your code

char pst[255];

main(int argc, char *argv[])
{
int by;

printf("\n\n\n");

for (by = 0; by < 2; by++)
printf("%s\n", szin(by));

return(0);
}
char szin(int w)
char *szin(int w)
{
char *c;

switch(w)
{
case 0 : strcpy(pst, "Unknown/incorrect placed Directive");
break;
case 1 : strcpy(pst, "Line ended incorrectly");
break;
default : strcpy(pst, "?????????");
}

c = pst;
return(*c);
return c;

Or just:

return pst;

Either will do.

<snip>
>
c = pst;

strcpy(pst, "abcdefgh");
c = pst;
printf("-1- %s\n", pst);
printf("-2- %c\n", *c);
printf("-3- %s\n", *c);
*c is a char, not a char *. %s expects a char *, not a char.
Still the question: why can't I return 'pst'?
You can.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 22 '08 #9
Nick Keighley <ni******************@hotmail.comwrote:
On 21 Oct, 15:32, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
Nick Keighley <nick_keighley_nos...@hotmail.comwrote:
On 21 Oct, 09:38, Ruud <Ruud.Baltis...@apg.nlwrote:
In my Pascal program I gathered all messages in one function. All
messages have their own number. One of the advantages of this method
is that if someone wants to change a messages, he only has to look in
one place.
and if you want to know what a message is it's never in the
place where you look...
Very long macro names can help.
=A0 =A0error (27);
and somewhere is
=A0 =A0 {27, "communications link is lost"}
hence a better macro might be
=A0 =A0 error (LINK_LOST);
I worked on a project where the error macros looked like this
=A0 =A0 error (SYSTEM_ERROR_COMMUNICATIONS_LINK_IS_LOST);
Or in this case, even better, use an enum.

why is an enum "better"?
It's cleaner, for starters. You have all your constants in one place
rather than a bunch of macros.

Richard
Oct 22 '08 #10
On 22 Oct, 09:25, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
Nick Keighley <nick_keighley_nos...@hotmail.comwrote:
On 21 Oct, 15:32, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
Nick Keighley <nick_keighley_nos...@hotmail.comwrote:
On 21 Oct, 09:38, Ruud <Ruud.Baltis...@apg.nlwrote:
In my Pascal program I gathered all messages in one function. All
messages have their own number. One of the advantages of this method
is that if someone wants to change a messages, he only has to look in
one place.
and if you want to know what a message is it's never in the
place where you look...
Very long macro names can help.
=A0 =A0error (27);
and somewhere is
=A0 =A0 {27, "communications link is lost"}
hence a better macro might be
=A0 =A0 error (LINK_LOST);
I worked on a project where the error macros looked like this
=A0 =A0 error (SYSTEM_ERROR_COMMUNICATIONS_LINK_IS_LOST);
Or in this case, even better, use an enum.
why is an enum "better"?

It's cleaner, for starters. You have all your constants in one place
rather than a bunch of macros.
I can put all the macros in one place.
Or I can have multiple "namespaces" with error
macros specific to different sub-systems
declared local to those systems. (I have seen
this done and spent a fair amount of time cursing
the lunatic who thought of it. grep is your fiend)

#define SYSTEM_ERROR_BASE 1000
#define SYSTEM_ERROR_GENERAL (SYSTEM_ERROR_BASE + 1)
#define SYSTEM_ERROR_WONT_START (SYSTEM_ERROR_BASE + 2)
#define SYSTEM_ERROR_HALT_BY_COMMAND (SYSTEM_ERROR_BASE + 3)
#define SYSTEM_ERROR_HALT_BY_TRAP (SYSTEM_ERROR_BASE + 4)
#define COMMS_ERROR_BASE (SYSTEM_ERROR_BASE + 10000)
#define COMMS_ERROR_LINK_LOST (COMMS_ERROR_BASE + 1)
#define COMMS_ERROR_BER_HIGH (COMMS_ERROR_BASE + 2)
#define COMMS_ERROR_FRAMING (COMMS_ERROR_BASE + 3)
#define COMMS_ERROR_UNKOWN_ADDRESS (COMMS_ERROR_BASE + 4)
--
Nick Keighley


Oct 22 '08 #11
Nick Keighley wrote:
On 22 Oct, 09:25, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
>Nick Keighley <nick_keighley_nos...@hotmail.comwrote:
>>On 21 Oct, 15:32, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
Nick Keighley <nick_keighley_nos...@hotmail.comwrote:
On 21 Oct, 09:38, Ruud <Ruud.Baltis...@apg.nlwrote:
>In my Pascal program I gathered all messages in one function. All
>messages have their own number. One of the advantages of this method
>is that if someone wants to change a messages, he only has to look in
>one place.
and if you want to know what a message is it's never in the
place where you look...
Very long macro names can help.
=A0 =A0error (27);
and somewhere is
=A0 =A0 {27, "communications link is lost"}
hence a better macro might be
=A0 =A0 error (LINK_LOST);
I worked on a project where the error macros looked like this
=A0 =A0 error (SYSTEM_ERROR_COMMUNICATIONS_LINK_IS_LOST);
Or in this case, even better, use an enum.
why is an enum "better"?
It's cleaner, for starters. You have all your constants in one place
rather than a bunch of macros.

I can put all the macros in one place.
Or I can have multiple "namespaces" with error
macros specific to different sub-systems
declared local to those systems. (I have seen
this done and spent a fair amount of time cursing
the lunatic who thought of it. grep is your fiend)

#define SYSTEM_ERROR_BASE 1000
#define SYSTEM_ERROR_GENERAL (SYSTEM_ERROR_BASE + 1)
#define SYSTEM_ERROR_WONT_START (SYSTEM_ERROR_BASE + 2)
#define SYSTEM_ERROR_HALT_BY_COMMAND (SYSTEM_ERROR_BASE + 3)
#define SYSTEM_ERROR_HALT_BY_TRAP (SYSTEM_ERROR_BASE + 4)
So that's cleaner and less error prone than

enum SystemError
{
SYSTEM_ERROR_BASE = 1000,
SYSTEM_ERROR_GENERAL,
SYSTEM_ERROR_WONT_START,
SYSTEM_ERROR_HALT_BY_COMMAND,
SYSTEM_ERROR_HALT_BY_TRAP
};

?

--
Ian Collins
Oct 22 '08 #12
Hallo Richard & Spiro,

char *szin(int w)
Spiro just emailed me the same in a PM. And this works!
Thank you both !!!

Having a good look at it it is all logical. 'printf' expects an
pointer and '*szin' returns one, something 'szin' didnit.
--
___
/ __|__
/ / |_/ Groetjes, Ruud
\ \__|_\
\___| URL: Ruud.C64.org

Oct 22 '08 #13
On 22 Oct, 10:28, Ian Collins <ian-n...@hotmail.comwrote:
Nick Keighley wrote:
On 22 Oct, 09:25, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
Nick Keighley <nick_keighley_nos...@hotmail.comwrote:
On 21 Oct, 15:32, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
Nick Keighley <nick_keighley_nos...@hotmail.comwrote:
On 21 Oct, 09:38, Ruud <Ruud.Baltis...@apg.nlwrote:
>>>>In my Pascal program I gathered all messages in one function. All
messages have their own number. One of the advantages of this method
is that if someone wants to change a messages, he only has to lookin
one place.
>>>and if you want to know what a message is it's never in the
place where you look...
Very long macro names can help.
=A0 =A0error (27);
and somewhere is
=A0 =A0 {27, "communications link is lost"}
hence a better macro might be
=A0 =A0 error (LINK_LOST);
I worked on a project where the error macros looked like this
=A0 =A0 error (SYSTEM_ERROR_COMMUNICATIONS_LINK_IS_LOST);
>>Or in this case, even better, use an enum.
>why is an enum "better"?
It's cleaner, for starters. You have all your constants in one place
rather than a bunch of macros.
I can put all the macros in one place.
Or I can have multiple "namespaces" with error
macros specific to different sub-systems
declared local to those systems. (I have seen
this done and spent a fair amount of time cursing
the lunatic who thought of it. grep is your fiend)
#define SYSTEM_ERROR_BASE 1000
#define SYSTEM_ERROR_GENERAL (SYSTEM_ERROR_BASE + 1)
#define SYSTEM_ERROR_WONT_START (SYSTEM_ERROR_BASE + 2)
#define SYSTEM_ERROR_HALT_BY_COMMAND (SYSTEM_ERROR_BASE + 3)
#define SYSTEM_ERROR_HALT_BY_TRAP (SYSTEM_ERROR_BASE + 4)

So that's cleaner and less error prone than

enum SystemError
{
* SYSTEM_ERROR_BASE = 1000,
* SYSTEM_ERROR_GENERAL,
* SYSTEM_ERROR_WONT_START,
* SYSTEM_ERROR_HALT_BY_COMMAND,
* SYSTEM_ERROR_HALT_BY_TRAP

};

?
ah, I should ahve stressed the second lot (the comms ones)
were in a different file (actaully different package)
from the general ones. Yes it has its downside but
on a large project there might be reasons for different
sub-systems to have semi-independent error list.

I reality some of this was code generated by a CASE tool

"cleaness" of code is another on of those opinion
thingies

--
Nick Keighley

A coding theorist is someone who doesn't think Alice is crazy.
Oct 22 '08 #14
On 22 Oct, 09:15, Ruud <Ruud.Baltis...@apg.nlwrote:
As I already mentioned, my original funtion 'zin' (FYI: Dutch for
'sentence')
there was I thinking it was named after the PTI in Star Ship Troopers!

--
Nick Keighley
Oct 22 '08 #15
Ruud wrote:
Hallo allemaal,
In my Pascal program I gathered all messages in one function. All
messages have their own number. One of the advantages of this method
is that if someone wants to change a messages, he only has to look in
one place.

The function is declared as follow:
function zin(by : byte) : string;

A line calling the function can look like as simple as
this:
writeln(zin(12));

So far I have found a replacement for this function but I'm not
satisfied with it. I defined a 'char pst[255]' and the function 'void
zin(int by)' fills pst with the wanted message. But that means I now
need two lines instead of one:
zin(12);
printf("%s", pst);

Of course I tried to turn 'void zin(int by)' into a funtion that
returns a value so I could combine those two line to something like
'printf("%s", zin(12));' but I either got an compilation error or only
nonsense appeared on the screen.

In the meanwhile I found a solution in the form of
char *zin[20] = { "..", ""..." etc. }
printf("%s", zin[12]);

But that works fine for fixed messages but what about messages that
are generated by a function on the fly?

Any help is welcome!
--
___
/ __|__
/ / |_/ Groetjes, Ruud Baltissen
\ \__|_\
\___| http://Ruud.C64.org

pascal:
function zin(by : byte) : string;

c++:
string zin(unsigned char by);
pascal
writeln(zin(12));

c++:

cout<<zin(12);
No pure C solution is available because pure C has no string type.
Oct 22 '08 #16
Nick Keighley wrote, On 22/10/08 11:54:
On 22 Oct, 10:28, Ian Collins <ian-n...@hotmail.comwrote:
>Nick Keighley wrote:
>>On 22 Oct, 09:25, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
Nick Keighley <nick_keighley_nos...@hotmail.comwrote:
On 21 Oct, 15:32, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
>Nick Keighley <nick_keighley_nos...@hotmail.comwrote:
>>On 21 Oct, 09:38, Ruud <Ruud.Baltis...@apg.nlwrote:
>>>>>>>In my Pascal program I gathered all messages in one function. All
>>>messages have their own number. One of the advantages of this method
>>>is that if someone wants to change a messages, he only has to look in
>>>one place.
>>and if you want to know what a message is it's never in the
>>place where you look...
>>Very long macro names can help.
>>=A0 =A0error (27);
>>and somewhere is
>>=A0 =A0 {27, "communications link is lost"}
>>hence a better macro might be
>>=A0 =A0 error (LINK_LOST);
>>I worked on a project where the error macros looked like this
>>=A0 =A0 error (SYSTEM_ERROR_COMMUNICATIONS_LINK_IS_LOST);
>Or in this case, even better, use an enum.
why is an enum "better"?
It's cleaner, for starters. You have all your constants in one place
rather than a bunch of macros.
I can put all the macros in one place.
Or I can have multiple "namespaces" with error
macros specific to different sub-systems
declared local to those systems. (I have seen
this done and spent a fair amount of time cursing
the lunatic who thought of it. grep is your fiend)
#define SYSTEM_ERROR_BASE 1000
#define SYSTEM_ERROR_GENERAL (SYSTEM_ERROR_BASE + 1)
#define SYSTEM_ERROR_WONT_START (SYSTEM_ERROR_BASE + 2)
#define SYSTEM_ERROR_HALT_BY_COMMAND (SYSTEM_ERROR_BASE + 3)
#define SYSTEM_ERROR_HALT_BY_TRAP (SYSTEM_ERROR_BASE + 4)
So that's cleaner and less error prone than

enum SystemError
{
SYSTEM_ERROR_BASE = 1000,
SYSTEM_ERROR_GENERAL,
SYSTEM_ERROR_WONT_START,
SYSTEM_ERROR_HALT_BY_COMMAND,
SYSTEM_ERROR_HALT_BY_TRAP

};

?

ah, I should ahve stressed the second lot (the comms ones)
were in a different file (actaully different package)
from the general ones.
That does not make the enum approach any harder...
enum CommsErrors {
COMMS_ERROR_BASE = SYSTEM_ERROR_BASE + 10000,
...
Yes it has its downside but
on a large project there might be reasons for different
sub-systems to have semi-independent error list.
For this type of thing as long as they are integers within the range
supported for enum on the implementation I find it incredibly hard to
see a situation where you could use the #define approach and not the
enum approach.
I reality some of this was code generated by a CASE tool
Well, with auto-generated code you often don't care about looks :-)
"cleaness" of code is another on of those opinion
thingies
Indeed. I prefer the enum approach myself though.
--
Flash Gordon
If spamming me sent it to sm**@spam.causeway.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Oct 22 '08 #17
Flash Gordon wrote:
Nick Keighley wrote, On 22/10/08 11:54:
>"cleaness" of code is another on of those opinion
thingies

Indeed. I prefer the enum approach myself though.
And don't forget if you use enums, you can compile your code with a C++
compiler to make sure you only use valid values :)

--
Ian Collins
Oct 22 '08 #18
Hallo Michael,
pascal:
function zin(by : byte) : string;

c++:
string zin(unsigned char by);

pascal
writeln(zin(12));

c++:

cout<<zin(12);

No pure C solution is available because pure C has no string type.
Very interesting indeed, thank you!

--
___
/ __|__
/ / |_/ Groetjes, Ruud
\ \__|_\
\___| URL: Ruud.C64.org
Oct 24 '08 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Chris Gordon-Smith | last post by:
I am tying to call a Pascal function from C++, and vice versa. Does anyone know how to do this, or where detailed information on this topic can be found? For the C++ to Pascal call I have...
24
by: Faith Dorell | last post by:
I really donīt like C.You can write better programs in BASIC than in C, if you donīt like this language. I donīt understand how C became so popular, although much better programming languages...
14
by: digital | last post by:
hello anyone... pls explain me , how different between function and procedure for C/C++ and Pascal. Thankx......
28
by: Skybuck Flying | last post by:
Hi, I think I understand now a bit better what the difference is between a c compiler and a pascal compiler. For example: When compiling source code with a pascal compiler. The pascal...
17
by: David Scemama | last post by:
Hi, I'm writing a program using VB.NET that needs to communicate with a DOS Pascal program than cannot be modified. The communication channel is through some file databases, and I have a huge...
6
by: kkrish | last post by:
hi, I am working on an old program written in c.The program uses a function like this "unsigned long int far pascal ReadFile(char *buff,unsigned long int *size)" . Is this a PASCAL...
15
by: jacob navia | last post by:
Programming languages come and go. Still is amazing that in this survey from http://www.devsource.com/article2/0,1895,2016936,00.asp the C language comes second, right after Java. Java # What...
0
by: dhruba.bandopadhyay | last post by:
Am using Borland C++ 4.5 for the old dos.h APIs. It appears that newer versions of compilers stop support for the oldskool DOS routines. Am trying to convert/port an oldskool Pascal program that...
7
by: SMALLp | last post by:
Hy! I desperately need help! I need to make application that would accept Pascal code and check if it returns good results. My idea is (from a beginner point of view) to make application in...
54
by: Ruud | last post by:
Hallo allemaal, During the conversion of my program from Pascal to C, I was more or less able to find the C equivalent of most Pascal functions so far. Only four gave me some real trouble. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.