[lug] An unusual C construct

Anthony Foiani tkil at scrye.com
Tue Sep 21 22:23:50 MDT 2010


Zan Lynx <zlynx at acm.org> writes:

> I think what [Bear] must have meant there is:
> 	snprintf(tbl->name, sizeof(tbl->name), "%s", new_name);

Yes, he said that he meant to use 'snprintf' in his reply.  That still
assumes that tbl->name is defined as an array; if it were declared as
a pointer, you get horrors like this:

| $ uname -a
| Linux localhost 2.6.34.6-47.fc13.x86_64 #1 SMP Fri Aug 27 08:56:01 UTC 2010 x86_64 x86_64 x86_64 GNU/Linux

| $ cat snprintf-test.c
| #include <stdio.h>
| 
| #define PRINT_SIZE( x ) \
|     printf( "sizeof( " #x " ) = %u\n", sizeof( x ) )
| 
| int
| main( int argc, char * argv [] )
| {
|     char * name_ptr;
|     char name_array[128];
| 
|     PRINT_SIZE( name_ptr );
|     PRINT_SIZE( name_array );
| 
|     return 0;
| }

| $ make snprintf-test
| cc     snprintf-test.c   -o snprintf-test

| $ ./snprintf-test
| sizeof( name_ptr ) = 8
| sizeof( name_array ) = 128

Anyway.  I think the point has been made.  :)

Thanks for helping keep me honest!

t.



More information about the LUG mailing list