home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19474 < prev    next >
Encoding:
Internet Message Format  |  1993-01-04  |  2.1 KB

  1. Xref: sparky comp.lang.c:19474 comp.lang.c++:18875
  2. Newsgroups: comp.lang.c,comp.lang.c++
  3. Path: sparky!uunet!spool.mu.edu!agate!usenet.ins.cwru.edu!ncoast!brown
  4. From: brown@NCoast.ORG (Stan Brown)
  5. Subject: Re: Dynamic Mem Help
  6. Organization: Oak Road Systems, Cleveland Ohio USA
  7. Date: Sat, 9 Jan 1993 14:11:07 GMT
  8. Message-ID: <C0LBEL.4r2@NCoast.ORG>
  9. References: <93007.230524U10139@uicvm.uic.edu> <rfries.159@sceng.ub.com>
  10. Lines: 44
  11.  
  12. In article <rfries.159@sceng.ub.com> rfries@sceng.ub.com (Robert Fries) writes:
  13. >In article <93007.230524U10139@uicvm.uic.edu> T J Furstenau <U10139@uicvm.uic.edu> writes:
  14. >... stuff deleted
  15. >>    char *str = NULL;
  16. >
  17. >>    /* allocate memory for string */
  18. >>    str = calloc(10, sizeof(char));
  19. >... more stuff deleted
  20. >>I get the following error message:
  21. >
  22. >>Error D:\BC\BIN\WORK\NONAME00.CPP 10: Cannot assign 'void near*' to
  23. >>'char near*' in function main().
  24. >
  25. >C++ and ANSI C do fairly strict type-checking, and since
  26. >malloc() and calloc() are used to allocate memory for
  27. >different types of objects, they are declared to return
  28. >(void *), which is sort of a generic pointer.
  29. >
  30. >You'll have to use an explicit cast of this value to 
  31. >assign it to a variable of type 'char *', e.g.:
  32. >    str = (char *)calloc(10, sizeof(char));
  33. >           ^^^^^^    
  34.  
  35. Red herring!
  36.  
  37. From the error message, I presume that the original questioner is
  38. programming in C++.  It was not the best idea to post this question to
  39. both comp.lang.c and comp.lang.c++, since this is one of the areas where, 
  40. it appears, C++ differs from C.  People will be posting followups from
  41. their newsgroups and not say whether they're talking C or C++.
  42.  
  43. I can't speak for C++, but the program is a correct C program with
  44. respect to the pointer assignment.  (To be an ANSI C program, it should
  45. include stdlib.h not alloc.h.) 
  46.  
  47. In ANSI C, void* is assignment-compatible with a pointer to any type of
  48. object, so the cast is unnecessary.  Reference:  ANSI Standard sec
  49. 3.2.2.3 (ISO section 6.2.2.3, I believe).
  50.  
  51. -- 
  52. Stan Brown, Oak Road Systems                      brown@Ncoast.ORG
  53. Cleveland, Ohio, USA
  54.  
  55. "Life!  Don't talk to me about life!"
  56.