Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!jarthur!nntp-server.caltech.edu!bruce From: bruce@seismo.gps.caltech.edu (Bruce Worden) Newsgroups: comp.lang.c Subject: char *'s and const char *'s (followup) Message-ID: <1991Jan16.234202.20933@nntp-server.caltech.edu> Date: 16 Jan 91 23:42:02 GMT References: <1991Jan11.182945.5437@nntp-server.caltech.edu> <14825@smoke.brl.mil> Sender: bruce@seismo.gps.caltech.edu (Bruce Worden) Organization: California Institute of Technology, Pasadena Lines: 33 Nntp-Posting-Host: sis.gps.caltech.edu Thanks to everyone who responded here and by e-mail. I got a number of interesting responses. Doug's was typical: In article <14825@smoke.brl.mil> gwyn@smoke.brl.mil (Doug Gwyn) writes: >In article <1991Jan11.182945.5437@nntp-server.caltech.edu> bruce@seismo.gps.caltech.edu (Bruce Worden) writes: >>Given: >>int foo(char *file); >>And knowing that foo() really only needs a const char *, it is often called as: >> a = foo("myfile"); >>but a kindly compiler will warn something like: >> junk.c:7: warning: argument passing of non-const * pointer from const * > >You should have declared foo as int foo(const char *file). No diagnostic >should be generated when a pointer to non-const char is passed as an >argument to the function, and of course no diagnostic would be issued >for a pointer to const char either. Which is, of course, the correct approach. However, when I wrote "Given:" I really meant "Given:". So based on the comments I got, I can nag the supplier of the code, a task which I will gleefully undertake. In the meantime I will use: char descriptive_name[] = "myfile"; ... a = foo(descriptive_name); ... As a number of people suggested. Thanks again. -------------------------------------------------------------------------- C. Bruce Worden bruce@seismo.gps.caltech.edu 252-21 Seismological Laboratory, Caltech, Pasadena, CA 91125