Finding Available Length Of Strings...

Gary Weimer weimer at ssd.kodak.com
Wed Nov 14 01:05:27 AEST 1990


In article <1990Nov09.183957.15122 at dirtydog.ima.isc.com> karl at ima.isc.com (Karl Heuer) writes:
>In article <16758 at hydra.gatech.EDU> gt4512c at prism.gatech.EDU (BRADBERRY,JOHN L) writes:
>>Just a note of clarification here...I am talking about a character array
>>and I am looking for a solution (not the obvious '...add another length
>>parameter')...I would like the function to be able to 'figure it out!'
>
>Here are the options that spring to mind:
>(a) Pass a length parameter.
>(b) Pass a pointer to the end of the string.
>(c) Implement a string structure that does one of the above for you, e.g.
>    typedef struct { char *start; char *current; char *end; } string_t;
>(d) Use only implementations that support the "Read Operator's Mind" syscall.

(e) Use the "standard" C workaround for this problem.

As other people have pointed out, this question looks like it was posed
by a C crossover from another language, so why don't we tell them what C
can do, instead of what it can't.

NOTE: for both solutions given below, don't forget to count the extra
space (byte, or whatever you want to call it) required by the end-of-string
character (\0).

EASY SOLUTION:

If all the strings you will be using are less than some number N (and
you have enough memory), then create a constant:

    #define MAX_LEN        N

where N can be any number greater than 0 (I like 255 for most cases). Now
define all your character arrays as:

    char name[MAX_LEN];

when performing loops, range checking, etc., use MAX_LEN 

ROBUST SOLUTION:

If you don't have a maximum length, or can't afford to waste memory, use
character pointers and malloc() memory as it is needed. This will allow
you to continue using the C string library; however, functions like strcat()
should probably be avoided (unless you malloc'd enough space for this). An
example (note I didn't say good) of a strcat() replacement is:

    char *mystrcat(char *s, char *t)
    {
        char *str;

        str = (char *) malloc(strlen(s) + strlen(t) + 1);
        /* should add check for str == NULL here (malloc() failed) */

        strcpy(str, s);
        strcat(str, t);
        /* NOTE: these next 2 statements disallow passing an */
        /*       array of char as s (use strcat() for this)  */
        free(s);
        s = str;

        return(s);
    }

With this solution, you will still want one string of some maximum size
to read in strings of unknown length. This could then be copied to a
string of the appropriate size (strdup() might be a good method):

    char *strdup(char *s) /* no, not a C library function */
    {
        char *str;
        str = (char *) malloc(strlen(s) + 1);
        strcpy(str, s);
        return(str);     /* notice that s is unchanged, and could */
                         /* have been declared: char s[MAX_LEN]   */
    }

(OH BOY, now I get to see how many people think this is stupid...)

Gary Weimer



More information about the Comp.lang.c mailing list