On 14/06/2008, at 12:55 AM, Lim Hock-Chai wrote:

Funny enough, I read an article yesterday that talks about c programmer
should try to avoid using typedef.

Why? Is it on-line? Do you have a link?


The reason that I use typedef is because the moduleName and locationID
should actually be a fix 10 char long variables. The logMessage should
actually be a varying length field of 2048 char long. So, to force all
caller to obey this rule, I used typedef and this was what it looked
like originally:
typedef struct {
char value[10];
} logECD_STP3_piModuleName_T;

typedef struct {
char value[32766];
} logECD_STP3_piLocationID_T;

typedef struct {
unsigned short l;
char a[2048];
} logECD_STP3_piLogMessage_T


That's a good use of typedef even though it's weird from a C programmer's perspective. You are indicating that the interface is not a "normal" C null-terminated string but something else so pay attention.


But I soon found out that c doesn't allow passing literal to a char
array parameter (<= Not programmer friendly).

Yes it does. As long as the prototype is correct. For example:

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

You can see the second call to func passes a string literal. The reason it didn't work for you was because the types were incompatible.

So I change it to use char * and left the typedef (lazy me).


Yep, but char* is not the right approach either.

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 / \
--------------------------------------------------------------------




This thread ...

Replies:

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

This mailing list archive is Copyright 1997-2019 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].