From: Dave Pearson Newsgroups: comp.os.msdos.djgpp Subject: Re: More string problems! Date: Thu, 28 Aug 1997 18:27:31 GMT Organization: Hagbard's World (A Private Internet Host) Sender: usenet AT hagbard DOT demon DOT co DOT uk Message-ID: References: <34058C10 DOT 254EBF66 AT phs DOT mat-su DOT k12 DOT ak DOT us> Reply-To: davep AT hagbard DOT demon DOT co DOT uk Lines: 105 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk On Thu, 28 Aug 1997 06:32:48 -0800, Kevin Dickerson wrote: > Allright, yesterday I posted up some code that would do some stuff > with strings (that isn't important), If you are talking about the message I think you are talking about, it is, there was some very sage advice and pointers in the followups. > but now I've encountered the main problem associated with it; it > strikes me as strange! There is nothing strange about the problem you have. > This code (which requires only ): > > main() > { > int l1; > char *chr, *buf, *text; Note that you declare a variable called buff which is a, as yet unused, pointer to some memory. Currently, no memory is allocated and pointed to by buf. Also, note that chr is the same. > text = "I Can't Believe It's Not Butter!"; Ok, the pointer called text points someplace. > for (l1=0; l1 { > sprintf( chr, "%c", text[l1] ); At this point you are mis-using the variable 'chr' which was declared as a pointer to some memory, now you are holding a char value in it. Ok, it will work, but it's not very clean and IMHO isn't good practice. > strcat( buf, chr ); Stop there! You are now attempting to append the string pointed to by chr (there isn't one) to the string pointed to by buf (there isn't one). I'd be suprised if this even ran. > } > printf("margarine = \"%s\"\n", buf ); Won't work either. 'buf' doesn't point to what you think it points to. > Memory allocation shouldn't be a problem, should it? This is exactly your problem, you haven't allocated any. > I'm inclined to believe that it's a problem with strcat(), but I'm > not positive. Argh! No, there is no problem with strcat(), there is a problem with your understanding of strcat(), sprintf() and printf(). > As I mentioned in my other message, I'm not a newcomer to C, but > rather to C's blasted string functions. Thanks for your help in > advance! Don't take this the wrong way, but if you are having this sort of problem then you need to learn a bit more about C. In many respects strings are nothing special in C, they are just memory. The only real special thing about strings is that there are a set of functions (the str*() functions and friends) that observe the convention of having a NUL value as the marker of a string. I would also suggest that the DJGPP group isn't the correct place for such questions. A good C book and a group like comp.lang.c might be in order. BTW, that isn't a flame, it's just a suggestion. FWIW, here is one way of doing what you appeared to be attempting: ---------------------------------------------------------------------- #include #include #include int main( void ) { char *text = "I Can't Believe It's Not Butter!"; char *buf = strdup( text ); printf( "margarine = \"%s\"\n", buf ); free( buf ); return( 0 ); } ---------------------------------------------------------------------- There are also many other methods. Have a read of the DJGPP libc info pages and play close attention to things like malloc(), free(), strdup(), strcpy(), etc..... -- Take a look in Hagbard's World: | w3ng - The WWW Norton Guide reader. http://www.acemake.com/hagbard/ | ng2html - The NG to HTML converter. http://www.hagbard.demon.co.uk/ | eg - Norton Guide reader for Linux. Free software, including........| dgscan - DGROUP scanner for Clipper.