Finding Available Length Of Strings...
Fraser Wilson
fraser at bilby.cs.uwa.oz.au
Sat Nov 10 20:45:38 AEST 1990
In <16752 at hydra.gatech.EDU> gt4512c at prism.gatech.EDU (BRADBERRY,JOHN L) writes:
>When passing strings to other functions, what is the BEST way to find
>the bytes remaining in the formal string parameter (to prevent over-
>writting the end while in the function)?? Does it involve using the
>current starting address of the string parameter and calculating
>(somehow) the DEFINED end??
The language supports no feature like this. All the function knows is
that it has a pointer to char. If you want to know how much space is
available, you have to do it yourself.
eg
#define LAST_CHARACTER '\1';
char s[32];
space(char *s)
{
int i;
for(i=0;s[i]!=LAST_CHARACTER;i++);
return i;
}
main()
{
s[31]=LAST_CHARACTER;
printf("%i\n",space(s));
}
>Thanks for any help here...
You're welcome. Hope it does.
Fraser.
More information about the Comp.lang.c
mailing list