Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!leah!rds95 From: rds95@leah.Albany.Edu (Robert Seals) Newsgroups: comp.lang.c Subject: String assignments; Was: When is a cast not a cast? Summary: String assignments Message-ID: <1814@leah.Albany.Edu> Date: 2 May 89 14:06:52 GMT References: <2747@buengc.BU.EDU> <13721@steinmetz.ge.com> Organization: The University at Albany, Computer Services Center Lines: 26 > In article <2747@buengc.BU.EDU> bph@buengc.bu.edu (Blair P. Houghton) writes: > [some stuff about pointers which has been conquered with aplomb by > Bill Davidson et al...but the stuff that gets me is...] > > | 3 char *c; > ... > | 7 c = "somestring"; /* Nothing fancy, null-terminated. */ Now I always thought this kind of thing was extremely bad form, but I've recently seen a couple of code examples that use this. One figures that if ``somestring'' is generated by the compiler somewhere, then the assignment will work as expected, because the address of it will be copied to ``c''. But what if the compiler decides that ``somestring'' has finished it's duty to the program, and overwrites the space it took with something else? Then ``c'' points to rubbish. Right? I've always used stuff like ``char *c = "string";'' which the compiler should know how to deal with, as it's an initializer, or something along the lines of char *c; c = (char *) malloc(something); strcpy(c, somethingelse); Have I been misguided all this time? rob