Is this the best (only?) way to give a fresh char *ptr a strlen of 0?
Code:ptr=malloc(0); free(ptr);
Is this the best (only?) way to give a fresh char *ptr a strlen of 0?
Code:ptr=malloc(0); free(ptr);
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
You need at least a buffer of 1. Then just set the first element to 0.
gg
So it has to malloc'd anyway? I might as well just use the free call then.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Just assign "" to it.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
From the standard
In other words, it could end up pointing anywhere. So strlen() would probably segfault and/or return unpredictable results.Originally Posted by 7.20.3 C99
The best way (IMO) is what Codeplug suggested:
It doesn't have to be on the heap either (which is pretty obvious )Code:char * ch = malloc(1); if(ch == NULL) { /* do something */ } *ch = '\0'; /* ... do whatever with ch, resize if need be */ free(ch);
Code:char nul = '\0'; char * ch = &nul; /* do whatever with ch */
Last edited by zacs7; 10-10-2008 at 12:12 AM.