home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c:19474 comp.lang.c++:18875
- Newsgroups: comp.lang.c,comp.lang.c++
- Path: sparky!uunet!spool.mu.edu!agate!usenet.ins.cwru.edu!ncoast!brown
- From: brown@NCoast.ORG (Stan Brown)
- Subject: Re: Dynamic Mem Help
- Organization: Oak Road Systems, Cleveland Ohio USA
- Date: Sat, 9 Jan 1993 14:11:07 GMT
- Message-ID: <C0LBEL.4r2@NCoast.ORG>
- References: <93007.230524U10139@uicvm.uic.edu> <rfries.159@sceng.ub.com>
- Lines: 44
-
- In article <rfries.159@sceng.ub.com> rfries@sceng.ub.com (Robert Fries) writes:
- >In article <93007.230524U10139@uicvm.uic.edu> T J Furstenau <U10139@uicvm.uic.edu> writes:
- >... stuff deleted
- >> char *str = NULL;
- >
- >> /* allocate memory for string */
- >> str = calloc(10, sizeof(char));
- >... more stuff deleted
- >>I get the following error message:
- >
- >>Error D:\BC\BIN\WORK\NONAME00.CPP 10: Cannot assign 'void near*' to
- >>'char near*' in function main().
- >
- >C++ and ANSI C do fairly strict type-checking, and since
- >malloc() and calloc() are used to allocate memory for
- >different types of objects, they are declared to return
- >(void *), which is sort of a generic pointer.
- >
- >You'll have to use an explicit cast of this value to
- >assign it to a variable of type 'char *', e.g.:
- > str = (char *)calloc(10, sizeof(char));
- > ^^^^^^
-
- Red herring!
-
- From the error message, I presume that the original questioner is
- programming in C++. It was not the best idea to post this question to
- both comp.lang.c and comp.lang.c++, since this is one of the areas where,
- it appears, C++ differs from C. People will be posting followups from
- their newsgroups and not say whether they're talking C or C++.
-
- I can't speak for C++, but the program is a correct C program with
- respect to the pointer assignment. (To be an ANSI C program, it should
- include stdlib.h not alloc.h.)
-
- In ANSI C, void* is assignment-compatible with a pointer to any type of
- object, so the cast is unnecessary. Reference: ANSI Standard sec
- 3.2.2.3 (ISO section 6.2.2.3, I believe).
-
- --
- Stan Brown, Oak Road Systems brown@Ncoast.ORG
- Cleveland, Ohio, USA
-
- "Life! Don't talk to me about life!"
-