Thanks Barbara. I simplified my function to make it easier for me to
ask the question. The actual function prototype looks like below:
typedef char *logECD_STP3_piModuleName_T;
typedef char *logECD_STP3_piLocationID_T;
typedef char *logECD_STP3_piLogMessage_T;
typedef int logECD_STP3_piByteRcv_T;
typedef char *logECD_STP3_piDataRcv_T;
decimal(15,0) logECD_STP3(
logECD_STP3_piModuleName_T moduleName,
logECD_STP3_piLocationID_T locationID,
logECD_STP3_piLogMessage_T logMessage,
const logECD_STP3_piByteRcv_T *byteRcv,
const logECD_STP3_piDataRcv_T *dataRcv);
logECDSTP3 is actually a RPG export procedure and the RPG prototype for
logECD_STP3 looks like below:
P logECD_STP3...
P B export
D pi 15 0
D piModuleName * options(*string) value
D piLocationID * options(*string) value
D piLogMessage * options(*string) value
D piByteRcv 10i 0 options(*nopass :*omit) const
D piDataRcv * options(*nopass :*omit)
D const
The c program is casting the UAM (<= Data from recv()) and passing it to
the 5th parameter of logECD_STP3. The 4th parameter tell logECD_STP3
how long the 5th parameter is.
c is really fun. It is not very programmer friendly language though.
Not sure how this language can be so successful till this day. :)
-----Original Message-----
From: c400-l-bounces+lim.hock-chai=usamobility.com@xxxxxxxxxxxx
[mailto:c400-l-bounces+lim.hock-chai=usamobility.com@xxxxxxxxxxxx] On
Behalf Of Barbara Morris
Sent: Thursday, June 12, 2008 6:52 PM
To: C400-L@xxxxxxxxxxxx
Subject: Re: [C400-L] Basic c question again - type casting
Lim Hock-Chai wrote:
...
I did below and it compile. Not sure if it will run correctly or not.
But at least it compiled :)
...
UAM * data;
logECD_STP((char **) &data);
As a general rule, you should aim to code without any casts. Sometimes
they are necessary, but you should do everything you can to avoid them.
As a hard-and-fast rule: Never code a cast unless you understand
_completely_ what the effect will be. You have to understand exactly
what the cast means in terms of the layout of storage it is describing,
and you have to understand exactly what is the actual storage that you
are casting. Only then can you assess whether the cast is valid.
Casting is basically a way to lie to the compiler, and that is A Bad
Thing to do. (I see that Marty already made this point, but I'm
re-making it anyways)
You are telling the compiler that your variable "data" is a "pointer to
character", but it is really a "pointer to UAM". Since you are only
passing 1 parameter to logECD_STP(), it is likely that the function is
treating the parameter as null-terminated string. It is equally
_unlikely_ that your variable "data" actually is a null-terminated
string.
As an Amazon Associate we earn from qualifying purchases.