home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18470 < prev    next >
Encoding:
Text File  |  1992-12-16  |  4.3 KB  |  122 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!ocsmd!ted
  3. From: ted@ocsmd.ocs.com (Ted Scott)
  4. Subject: Re: character pointers in funtion calls
  5. Message-ID: <BzD1w3.1z5@ocsmd.ocs.com>
  6. Sender: news@ocsmd.ocs.com
  7. Organization: Online Computer Systems, Inc.
  8. X-Newsreader: Tin 1.1 PL5
  9. References: <BzC5oG.HpB@ucunix.san.uc.edu>
  10. Date: Wed, 16 Dec 1992 16:31:13 GMT
  11. Lines: 109
  12.  
  13. Spandan Choudury (schoudhu@ucunix.san.uc.edu) wrote:
  14. : This may be a very simple question (I must be missing something I am sure,)
  15. Uh Huh.
  16. : but I really do not have the time to figure out to get rid of warnings as
  17. Assignment due soon?
  18. : happen with attempting to call a function to return and assign character
  19. : type pointer variables as in the example below appars with some system level 
  20.                                                              ^^^^^^^^^^^^^^^^
  21. Got yer drives backed up?  
  22. : C code that I am currently designing (actually in MINIX C on a 386, but I 
  23. : thought a C related newsgroup will be more appropriate to discuss this
  24. : matter in).
  25. : I am not looking for compiler options/flags to skip compilation warnigs.
  26. Ok.
  27. : I am looking only for code improvements.
  28. <sigh>
  29. : (Please mail.)
  30. <heavy sigh> Well, it *is* Christmas ;-) But I won't mail.
  31. : ..........................
  32.  
  33. Step 1: Get a good C primer and look at the memory sections, then walk
  34. through the code you present here and see if you can find some of these 
  35. problems:
  36.  
  37. : #include <stdio.h>       <- very good 
  38. : main()                   <- another useful construct, might want to 
  39.                               return an int though, maybe accept argc,argv
  40. : {
  41. :    char* c;              <- *THINK* about what you've allocated here
  42.                               (storage for a pointer)
  43.  
  44. :    c = f();              <- f doesn't return a char *, I'm not sure 
  45.                               if f returns anything but if it does its an
  46.                               int, also you probably get an implicit 
  47.                               declaration warning here?
  48.  
  49. :    printf ("%s\n", c);   <- If you get c to point to the right thing,
  50.                               this will work.
  51.  
  52. : }                        <- All in all a fine first attempt. You might 
  53.                               want to allocate some storage and pass it 
  54.                               into f.
  55. :  f()                     <- If you want this to return a char * then
  56.                               define it that way: char *f() 
  57. : {
  58. :   char* i;               <- Look up "automatic variables" in the primer 
  59.                               you bought in step 1. Notice the part about 
  60.                               the storage becomming undefined when the 
  61.                               function exits.
  62.  
  63. :   i = "phoo";            <- A dangerous practice in the real world if
  64.                               you can't guarantee that *i will never be
  65.                               attempted to be modified.
  66.  
  67. :   return (i);            <- (and implicitly free the memory on the stack
  68.                               where i had a happy albiet brief life) 
  69.                               returning the old value of i which may or may 
  70.                               not be overwritten when printf uses the stack. 
  71. : }
  72.  
  73.  
  74. Try something like:
  75.  
  76. #include <stdio.h>
  77.  
  78. char  *phideaux(char *foo, int size) {
  79.  
  80.  
  81.    if (size < 5) return (NULL);
  82.    
  83.    if (!foo) if ((foo = (char *) malloc(size)) == NULL)  exit(1);
  84.    /* people who write code like this should be taken behind the woodshed... */
  85.  
  86.    memcpy(foo,"phoo",5);
  87.    return(foo);
  88. }
  89.  
  90. int main(int argc,char *argv) {
  91.    
  92.     char *c;
  93.  
  94.     if ((c = phideaux(c,4)) != NULL) {
  95.        printf("%s\n",c);
  96.        return(0);
  97.     }
  98.     
  99.     fprintf(stderr,"phideaux is a dog function that contains holes "
  100.                    "big enough to drive a Mack (tm) truck through!\n"
  101.                    "and therefore functions that manipulate storage "
  102.                    "should be used within the context that they were "
  103.                    "designed for and should not be violated. Otherwise "
  104.                    "they have every right to overwrite your inode table "
  105.                    "with a verbose error message acknowledging the "
  106.                    "\"splendorifious stupiditity\" of the implementor!");
  107.  
  108.    return (1);
  109. }
  110.  
  111.  
  112.     
  113. --
  114.  
  115. -Ted Scott          
  116. tscott@ocsmd.ocs.com           I was told that I'm a P.C. person:
  117. (301) 601-2252                               Politically Challenged, that is.
  118.