home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!ocsmd!ted
- From: ted@ocsmd.ocs.com (Ted Scott)
- Subject: Re: character pointers in funtion calls
- Message-ID: <BzD1w3.1z5@ocsmd.ocs.com>
- Sender: news@ocsmd.ocs.com
- Organization: Online Computer Systems, Inc.
- X-Newsreader: Tin 1.1 PL5
- References: <BzC5oG.HpB@ucunix.san.uc.edu>
- Date: Wed, 16 Dec 1992 16:31:13 GMT
- Lines: 109
-
- Spandan Choudury (schoudhu@ucunix.san.uc.edu) wrote:
- : This may be a very simple question (I must be missing something I am sure,)
- Uh Huh.
- : but I really do not have the time to figure out to get rid of warnings as
- Assignment due soon?
- : happen with attempting to call a function to return and assign character
- : type pointer variables as in the example below appars with some system level
- ^^^^^^^^^^^^^^^^
- Got yer drives backed up?
- : C code that I am currently designing (actually in MINIX C on a 386, but I
- : thought a C related newsgroup will be more appropriate to discuss this
- : matter in).
- :
- : I am not looking for compiler options/flags to skip compilation warnigs.
- Ok.
- : I am looking only for code improvements.
- <sigh>
- :
- : (Please mail.)
- <heavy sigh> Well, it *is* Christmas ;-) But I won't mail.
- : ..........................
- :
-
- Step 1: Get a good C primer and look at the memory sections, then walk
- through the code you present here and see if you can find some of these
- problems:
-
- : #include <stdio.h> <- very good
- : main() <- another useful construct, might want to
- return an int though, maybe accept argc,argv
- : {
- : char* c; <- *THINK* about what you've allocated here
- (storage for a pointer)
-
- : c = f(); <- f doesn't return a char *, I'm not sure
- if f returns anything but if it does its an
- int, also you probably get an implicit
- declaration warning here?
-
- : printf ("%s\n", c); <- If you get c to point to the right thing,
- this will work.
-
- : } <- All in all a fine first attempt. You might
- want to allocate some storage and pass it
- into f.
- :
- : f() <- If you want this to return a char * then
- define it that way: char *f()
- : {
- : char* i; <- Look up "automatic variables" in the primer
- you bought in step 1. Notice the part about
- the storage becomming undefined when the
- function exits.
-
- : i = "phoo"; <- A dangerous practice in the real world if
- you can't guarantee that *i will never be
- attempted to be modified.
-
- : return (i); <- (and implicitly free the memory on the stack
- where i had a happy albiet brief life)
- returning the old value of i which may or may
- not be overwritten when printf uses the stack.
- : }
-
-
- Try something like:
-
- #include <stdio.h>
-
- char *phideaux(char *foo, int size) {
-
-
- if (size < 5) return (NULL);
-
- if (!foo) if ((foo = (char *) malloc(size)) == NULL) exit(1);
- /* people who write code like this should be taken behind the woodshed... */
-
- memcpy(foo,"phoo",5);
- return(foo);
- }
-
- int main(int argc,char *argv) {
-
- char *c;
-
- if ((c = phideaux(c,4)) != NULL) {
- printf("%s\n",c);
- return(0);
- }
-
- fprintf(stderr,"phideaux is a dog function that contains holes "
- "big enough to drive a Mack (tm) truck through!\n"
- "and therefore functions that manipulate storage "
- "should be used within the context that they were "
- "designed for and should not be violated. Otherwise "
- "they have every right to overwrite your inode table "
- "with a verbose error message acknowledging the "
- "\"splendorifious stupiditity\" of the implementor!");
-
- return (1);
- }
-
-
-
- --
-
- -Ted Scott
- tscott@ocsmd.ocs.com I was told that I'm a P.C. person:
- (301) 601-2252 Politically Challenged, that is.
-