Path: utzoo!censor!geac!torsqnt!news-server.csri.toronto.edu!cs.utexas.edu!uunet!bu.edu!snorkelwacker!mit-eddie!uw-beaver!cornell!rochester!kodak!ispd-newsserver!weimer From: weimer@ssd.kodak.com (Gary Weimer) Newsgroups: comp.lang.c Subject: Re: Finding Available Length Of Strings... Message-ID: <1990Nov13.140527.14797@ssd.kodak.com> Date: 13 Nov 90 14:05:27 GMT References: <16752@hydra.gatech.EDU> <16758@hydra.gatech.EDU> <1990Nov09.183957.15122@dirtydog.ima.isc.com> Sender: news@ssd.kodak.com Organization: Eastman Kodak Lines: 78 In article <1990Nov09.183957.15122@dirtydog.ima.isc.com> karl@ima.isc.com (Karl Heuer) writes: >In article <16758@hydra.gatech.EDU> gt4512c@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