ok@quintus.UUCP (Richard A. O'Keefe) (03/24/88)
In article <12602@brl-adm.ARPA>, V053MF43@ubvmsc.cc.buffalo.EDU (Mike Ayers) writes:
: No one gave me a solution last time, so here I am again to state the
: problem a little more clearly. Thanks to a new utility, you can see firsthand
: the kind of frustration I'm getting here:
: $type q.c
: char *wr(a)
: int a;
: {
: char b = "Arf!";
: return(b);
: }
... stuff deleted ...
Unless there is a typo, the mistake is in the line
char b = "Arf!";
It should be
char *b = "Arf!";
At any rate, adding that one asterisk made the program work on a SUN.
barmar@think.COM (Barry Margolin) (03/24/88)
In article <12602@brl-adm.ARPA> V053MF43@ubvmsc.cc.buffalo.EDU (Mike Ayers) writes: >$type q.c >char *wr(a) >int a; >{ >char b = "Arf!"; > >return(b); >} I'm surprised this even compiled. The type of b is char, but the type of "Arf!" is char*, and so is the type of the return value of wr. You should declare it char *b = "Arf!"; Barry Margolin Thinking Machines Corp. barmar@think.com uunet!think!barmar
loafman@convex.UUCP (03/25/88)
> /* Written 8:54 pm Mar 23, 1988 by V053MF43@ubvmsc.cc.buffalo.EDU */ > /* ---------- "VMS pointer problems continue." ---------- */ > > char *wr(a) > int a; > { > char b = "Arf!"; > > return(b); > } > > main() > { > printf(" %s ",wr(4)); > } > > /* End of text from convex:comp.lang.c */ Actually there ARE a couple of problems: - char b = "..." is attempting to assign a pointer to a character. - char b is an automatic variable. Destroyed when function returns. - wr() returns a pointer. The return(b) returns a char. Garbage. - the printf %s wants a pointer. wr() returns a char not a pointer. Not surprised the thing barfed. Try running lint. I'm surprised the compiler did not complain at great lengths. Most would. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Kenneth W. Loafman @ CONVEX Computer Corp, Dallas, Texas | All opinions Voice: work: (214) 952-0829 home: (214) 964-1250 | are my own, USPSnail: 1705 Coit Rd #2101, Plano, Tx 75075 | of course. UUCP: ihnp4!convex!loafman | CompuServe: 72345,233 | ...KWL ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
maart@cs.vu.nl (Maarten Litmaath) (03/25/88)
In article <12602@brl-adm.ARPA> V053MF43@ubvmsc.cc.buffalo.EDU (Mike Ayers) writes:
\char *wr(a)
\int a;
\{
\char b = "Arf!";
\
\return(b);
\}
\
\main()
\{
\ printf(" %s ",wr(4));
\}
Two things:
1)
char *b = "Arf!"; /* notice the star before "b" */
2)
char *wr(); /* in "main": always declare your functions! */
Regards.
--
South-Africa: |Maarten Litmaath @ Free U Amsterdam:
revival of the Third Reich |maart@cs.vu.nl, mcvax!botter!ark!maart
V053MF43@ubvmsc.cc.buffalo.EDU (Mike Ayers) (03/25/88)
No one gave me a solution last time, so here I am again to state the problem a little more clearly. Thanks to a new utility, you can see firsthand the kind of frustration I'm getting here: =============================================================================== $type q.c char *wr(a) int a; { char b = "Arf!"; return(b); } main() { printf(" %s ",wr(4)); } $cc q $link q,sys$share:vaxcrtl-options-file/opt $run q %SYSTEM-F-ACCVIO, access violation, reason mask=00, virtual address=00000000,u[MF|L PC =00005438, PSL=0BC00005 %TRACE-F-TRACEBACK, symbolic stack dump follows module name routine name line rel PC abs PC 00005438 00005438 00002EC8 00002EC8 Q main 11 00000027 00000437 $type sys$share:vaxcrtl-options-file.opt Sys$Share:Vaxcrtl.Exe/Share =============================================================================== I am certain that this code is perfectly legal within K&R syntax rules. The error here seems to be that I am linking the wrong way. I am under VMS 4.7, and all the C linking rules have been changed. If anyone knows what I am doing wrong here (I am trying simply to get my function to return a pointer to the first character in a string - the function declaration I am using is the same style K&R used for their fgets) PLEASE let me know - this is getting downright aggravating. By the way, this code was ported to Turbo C where it also refused to compile properly - the best I've gotten under any system was the same garbage every time I run it. Has someone changed all the declaration standards while I wasn't looking? Me again . . . Mike Ayers /|___/| / , _ | INTERNET: V053MF43@UBVMS.BITNET or | O O / V053MF43@UBVMS.CC.BUFFALO.EDU / / SNAILNET: 190 Minnesota Ave. |\ / ARF! Buffalo, NY 14214 --- BELLNET : (716)838-3696 U "I am a jelly doughnut." - John F. Kennedy
jgm@K.GP.CS.CMU.EDU (John Myers) (03/25/88)
In article <1494@se-sd.sandiego.NCR.COM> rns@se-sd.sandiego.NCR.COM (Rick Schubert) writes:
:>Although Richard A. O'Keefe has pointed out the primary error in this
:>program, and probably the one that caused Mike Ayers' problem, there is
:>another error, although most implementations of C will handle it in the
:>intended way (notice that I did not say "the right way"). The string
:>pointed to by b ("Arf!") should be considered local to wr(); the compiler
:>is free to allocate it on the stack [...]
Wrong. String constants have storage class "static" according to K&R
Appendix A, section 2.5. Please check your references before you post.
--
_.John G. Myers Internet: John.Myers@cs.cmu.edu
LoseNet: ...!seismo!hao!wiscvm.wisc.edu!k!nobody
chris@mimsy.UUCP (Chris Torek) (03/26/88)
In article <1494@se-sd.sandiego.NCR.COM> rns@se-sd.sandiego.NCR.COM (Rick Schubert) writes: >Although Richard A. O'Keefe has pointed out the primary error in [ char *wr(a) int a; { char b = "Arf!"; return (b); } /* should be char *b = ... */ /* (this error should cause at least a warning at compile time) */ >] ... there is another error, although most implementations of C >will handle it in the intended way (notice that I did not say "the >right way"). The string pointed to by b ("Arf!") should be considered >local to wr(); the compiler is free to allocate it on the stack . . . . This is not the case. String constants have type `array N of char' and static storage duration. These are the only anonymous aggregate types that exist in C. Writing char *wr() { return ("Arf!"); } is essentially equivlaent to writing char *wr() { static char _not_named_[5] = { 'A', 'r', 'f', '!', '\0' }; return (_not_named_); } >I submitted the above, but inews asked for more; rather than just filling >the required space, I'll try to make efficient use of it by giving >one example of <<interesting>> behavior. [deleted] Well, at least *someone* had the sense to fill with something other than `filler' lines. You can also change the quote character to something other than `>'. E.g., in vi, type :%s/^>/-/ -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris
flaps@dgp.toronto.edu (Alan J Rosenthal) (03/27/88)
Discussing this code: >> char *wr() >> { >> char *b = "Arf!"; >> return(b); >> } rns@se-sd.sandiego.NCR.COM (Rick Schubert) writes: >The string pointed to by b ("Arf!") should be considered local to >wr(); the compiler is free to allocate it on the stack ... Thus the >value returned from wr() is a pointer to storage that is no longer in >scope (i.e. may be in an obsolete stack frame). Blatantly false! K&R, page 181, section 2.5: A string has type ``array of characters'' and storage class static (see \S4 below) and is initialized with the given characters. [ the `\S' means a section reference symbol ] >From the 11 January 1988 ANSI C Draft, section 3.1.4, page 31, line 25: A character string literal has static storage duration and type ``array of char,'' and is initialized with the given characters. [ they put that comma inside the quotes, not me ] Now, there may be compilers that do what you say, but (all together now) THAT AIN'T C. ajr -- "Comment, Spock?" "Very bad poetry, Captain."
rns@se-sd.sandiego.NCR.COM (Rick Schubert) (03/28/88)
In article <1223@PT.CS.CMU.EDU> jgm@K.GP.CS.CMU.EDU (John Myers) writes: >In article <1494@se-sd.sandiego.NCR.COM> rns@se-sd.sandiego.NCR.COM (Rick Schubert) writes: ^ That's me >: ... >: The string >:>pointed to by b ("Arf!") should be considered local to wr(); the compiler >:>is free to allocate it on the stack [...] > >Wrong. String constants have storage class "static" according to K&R >Appendix A, section 2.5. Please check your references before you post. Sorry -- You and several people who sent me mail on this are right, and I was wrong. I didn't check K&R or the current ANSI Draft (Section 3.1.4) before posting. I cancelled my posting after receiving the first mail, and I was hoping my erroneous posting wouldn't go too far. I'll try to be more careful next time. >_.John G. Myers Internet: John.Myers@cs.cmu.edu -- Rick Schubert (I'm not always wrong)
chris@ssbell.UUCP (Chris Olson) (03/31/88)
[This line is for line-eaters :-] In article <12702@brl-adm.ARPA> Russell_Green.WBST129@Xerox.COM writes: >If you change your declaration for b to: > char *b = "Arf!"; >you should have no further problems. > >You apparently wanted a pointer to a character string, you were returning the >whole string, not a pointer to it. Some compilers are VERY picky! > >Good luck, > >Russ The reason was wrong. The solution is correct! Hmm, what was really happening here was this: Please store the 4-byte quantity representing the address of the string "Arf!" into the 1 byte quantity represented by the variable b. Since there is no room for this large quantity, please truncate to fit. Needless to say, on a VAX under VMS, the first page of memory (0x00 - 0xff) is off limits to non-priviledged processes. Remember: "Pointers are the root off all evil in C, and also the root of all successes!" [+][+][+][+][+][+][+][+][+][+][+][+][+][+][+][+][+][+][+][+][+][+][+][+][+] Chris Olson O (aka, Lord Valentine, OXXXXI>>>>>>>>>>>>>>>>>>>>>>>> Electric Samurai, O & other foolishness) (chris@cbosgd!ohgua!ugn!ssbell)
Russell_Green.WBST129@Xerox.COM (03/31/88)
If you change your declaration for b to: char *b = "Arf!"; you should have no further problems. You apparently wanted a pointer to a character string, you were returning the whole string, not a pointer to it. Some compilers are VERY picky! Good luck, Russ