× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.




On 14/06/2008, at 2:29 AM, Lim Hock-Chai wrote:

I'm a bit lost. You say my RPG prototype should be 10A, not *string.
But your example showing RPG protype with *string.



I really prefer to do
typedef struct {
char value[10];
} logECD_STP3_piModuleName_T;

But I don't want to have to do below in c just to call a function:
memset(&piModuleName, ' ', sizeof(piModuleName));
memcpy(&piModuleName, "MYMODULE",8);
logECDSTP3(piModuleName...)

You have to do that because C provides no help with fixed-length variables. Note that C itself also provides no help with null- terminated strings. All the string functions used throughout C are library functions and part of the LANGUAGE specification. Therefore you could provide your own library of functions to deal with fixed- length data. This would have the effect of hiding the memset/memcpy pairs and if written properly could handle literal values too.

Simple pre-processor macros might help too:

#define CLEAR( s ) \
memset( (void *)&(s), 0x40, sizeof(s) )
#define COPY_STR( s, t ) \
memcpy( (void *)&(t), (void*)&(s), strlen(s) )
#define COPY_FXD( s, t ) \
memcpy( (void *)&(t), (void*)&(s), sizeof(s) )

You could combine these to get copy-with-pad functions.

Note: Not tested to use at own risk.

However, you can only do so much with macros and a set of fixed- length library functions would be better.


I want to do this, hence, I changed it to use *string
logECDSTP3("MYMODULE", ...)

You can do this by casting the literal to the correct type:

#include <stdio.h>
typedef struct {
char msg[10];
} Msg_T;
void func( Msg_T* );
void main( void )
{
Msg_T* text = {(Msg_T*)"test data"};
func( text );
func( (Msg_T*)"more test data" );
return;
}
void func( Msg_T* data )
{
printf( "%s\n", data );
return;
}

Anyone notice the defects in this code?

It works but it's not correct because:
a) printf() prints more than it should for the second call to func()
b) data has a null terminator on the first call because "test data" is shorter than the 10 bytes defined in text.msg so it's not fixed- length
c) data has no null terminator on the second call because "more test data" is longer than the 10 bytes defined thus printf() reads beyond the data.msg area

It's also ugly because of the casts between types. Sometimes necessary but should be avoided if possible.

In addition there are problems introduced by mixing fixed-length character data with functions expecting null-terminated strings. Specifying something like:

printf(%*.*s\n", sizeof(data->msg), sizeof(data->msg), data->msg);

may work but again is ugly.


Clear as mud?

Oh yes indeed.

Regards,
Simon Coulter.
--------------------------------------------------------------------
FlyByNight Software OS/400, i5/OS Technical Specialists

http://www.flybynight.com.au/
Phone: +61 2 6657 8251 Mobile: +61 0411 091 400 /"\
Fax: +61 2 6657 8251 \ /
X
ASCII Ribbon campaign against HTML E-Mail / \
--------------------------------------------------------------------




As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.