Why won't this code compile? 
Author Message
 Why won't this code compile?

I am trying my hand at dynamic allocation for arrays.  I checked in Mastering Borland C++ (Swan, 1992) on pages 294-295 (Dynamic Arrays) and tried the following code but can't get it to compile:

*/

#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <stdlib.h>

/*------------------------------------------------------------------*/

void main(void)
{  /* begin main  */
double *RandomArray;
int arraysize;

 /*---------- Clear screen, display time and date on output --------*/

 clrscr();

 time_t thetime = time(NULL);
 printf("%s\n", ctime(&thetime));

 /*----------------------- Main block of code ----------------------*/

 printf ("How big is the array? \n");
 scanf ("%d", &arraysize);
 RandomArray = malloc(arraysize * sizeof(double));
 }

I keep getting the following error:

        Cannot convert 'void *' to 'double *'

Please, any C/C++ gurus have a clue?
Thanks.

-Dan.



Wed, 18 Sep 1996 10:50:09 GMT  
 Why won't this code compile?

[....]

Quote:
>void main(void)
>{  /* begin main  */
>double *RandomArray;

^^^^^^^^^^^^^^^^^^^^^

Quote:
>int arraysize;
> /*---------- Clear screen, display time and date on output --------*/
> clrscr();
> time_t thetime = time(NULL);
> printf("%s\n", ctime(&thetime));
> /*----------------------- Main block of code ----------------------*/
> printf ("How big is the array? \n");
> scanf ("%d", &arraysize);
> RandomArray = malloc(arraysize * sizeof(double));

  ^^^^^^^^^^^^^^^^^^^^^

Quote:
> }
>I keep getting the following error:
>    Cannot convert 'void *' to 'double *'

The reason for your problem is verry simple!
The Prototype of malloc is like this:

void * malloc(size_t size).

So malloc returns a void pointer to the allocated memory. This is because
malloc doesnt know anything about the type for the allocated memory at
runtime. You must cast the returned pointer to convert it into the type of
the pointer variable you assign its value. In your case change the
call of malloc to:

Quote:
> RandomArray = (double *) malloc(arraysize * sizeof(double));  

This should solve your Problem.

Bye
  Ralph

--

voice: +49 431 82761



Wed, 18 Sep 1996 23:34:55 GMT  
 Why won't this code compile?

Quote:

>I am trying my hand at dynamic allocation for arrays.  I checked in Mastering Borland C++ (Swan, 1992) on pages 294-295 (Dynamic Arrays) and tried the following code but can't get it to compile:

<most of the code deleted for space conservation.>

Quote:
> RandomArray = malloc(arraysize * sizeof(double));
>I keep getting the following error:
>    Cannot convert 'void *' to 'double *'
>Please, any C/C++ gurus have a clue?

Well, I don't profess to be a guru, but the line above should be:
 RandomArray = (double *) malloc(arraysize * sizeof(double));

This is something you have to do for all malloc calls, as far as I know,
always put the cast for the type of thing you're allocating memory for.

Quote:
>Thanks.

No prob.

--
Charles W Blumreich III


Anyone who claims him/her self as God, clearly has an ego problem.
Anyone who claims him/her self as demon spawn clearly needs adjustment.



Thu, 19 Sep 1996 00:00:24 GMT  
 Why won't this code compile?
[ slightly edited to get rid of very long lines and just kept
  the relevant code fragments ... ]


|I am trying my hand at dynamic allocation for arrays.  I checked in
|Mastering Borland C++ (Swan, 1992) on pages 294-295 (Dynamic Arrays)
|and tried the following code but can't get it to compile:
|
|       #include <stdlib.h>
|
|       double *RandomArray;
|
|       RandomArray = malloc(arraysize * sizeof(double));
|
|I keep getting the following error:
|
|       Cannot convert 'void *' to 'double *'
|
|Please, any C/C++ gurus have a clue?

Although I do have very long hair, I don't consider myself a guru,
but I _do_ have a clue: call Borland and start complaining about
this behavior of your compiler. Have a look at what the standard
says about this:

ISO 6.2.2.3 Pointers (ANSI 3.2.2.3)

A pointer to void may be converted to or from a pointer to any
incomplete or object type. A pointer to any incomplete or object
type may be converted to a pointer to void and back again; the
result shall compare equal to the original pointer.

[ end quote ]

The malloc() function (correctly prototyped in stdlib.h) returns
a void pointer (void*). The assignment statement includes an
implicit cast to a double pointer (double*). The code snippet
above is correct and conformant. Any conformant compiler implementation
is allowed to whine about almost everything, but it is not allowed
to generate an error diagnostic for just this. For the time being,
you can explicitely cast the return value of the malloc() function.
Maybe that'll help you out:

        RandomArray= (double*)malloc(arraysize * sizeof(double));

But like I said: register a complaint and try to get your money back.

kind regards,


----------------------------------------------------------------------------
If I ever become empress of the world, I'm gonna rule out red cabbage.



Thu, 19 Sep 1996 18:54:44 GMT  
 Why won't this code compile?

Quote:


>|I am trying my hand at dynamic allocation for arrays.  I checked in
>|Mastering Borland C++ (Swan, 1992) on pages 294-295 (Dynamic Arrays)
>|and tried the following code but can't get it to compile:
>|
>|   #include <stdlib.h>
>|   double *RandomArray;
>|   RandomArray = malloc(arraysize * sizeof(double));
>|
>|I keep getting the following error:
>|
>|   Cannot convert 'void *' to 'double *'

|ISO 6.2.2.3 Pointers (ANSI 3.2.2.3)
|
|A pointer to void may be converted to or from a pointer to any
|incomplete or object type. A pointer to any incomplete or object
|type may be converted to a pointer to void and back again; the
|result shall compare equal to the original pointer.

I apologize for this follow-up, but I just noticed that the original
poster (Dan Mouer) asked the same question in comp.lang.c++ as well
as in comp.std.c and comp.std.c++. The original posting wasn't a
proper crosspost in comp.lang.c, so I assumed we were talking C here.

Obviously, we're talking C++ here, so my answer (see above) is dead wrong.
Using C++ the rules are slightly different. One cannot implicitly cast
a void pointer to a double pointer. Use an explicit cast instead and
forget my remark about complaining about the compiler.

Again, I apologize for ruining the bandwidth ...

kind regards,


----------------------------------------------------------------------------
If I ever become empress of the world, I'm gonna rule out red cabbage.



Thu, 19 Sep 1996 19:24:08 GMT  
 Why won't this code compile?

Quote:
> void main(void)

This should really be 'int main (void)'...No biggie as far as your problem here
is concerned.

Quote:
> double *RandomArray;

[code deleted]

Quote:
> RandomArray = malloc(arraysize * sizeof(double));

[code deleted]

Quote:
> I keep getting the following error:

>    Cannot convert 'void *' to 'double *'

Just do:

RandomArray = (double *) malloc (arraysize * sizeof (double));

The explicit cast should solve that problem.
--




Fri, 20 Sep 1996 02:03:26 GMT  
 Why won't this code compile?

Quote:
>void main(void)

This should be 'int main(void)'.  'void' is not an allowed return-type for
main.

Quote:
>{  /* begin main  */

Yeah, that's clear...

Quote:
>double *RandomArray;
>int arraysize;
> printf ("How big is the array? \n");
> scanf ("%d", &arraysize);
> RandomArray = malloc(arraysize * sizeof(double));

malloc() returns a void-pointer, which you then assign to a double-pointer...
To get rid of the error-message (and for the sake of decent code-writing in
general :-), insert the appropriate cast:
        RandomArray = (double *) malloc(arraysize * sizeof(double));

--

University of Utrecht                     The Netherlands

                  This message was passed by value



Sat, 21 Sep 1996 00:15:02 GMT  
 Why won't this code compile?

   >double *RandomArray;

   > RandomArray = malloc(arraysize * sizeof(double));

   malloc() returns a void-pointer, which you then assign to a double-pointer..
   To get rid of the error-message (and for the sake of decent code-writing in
   general :-), insert the appropriate cast:

           RandomArray = (double *) malloc(arraysize * sizeof(double));

If this is supposed to be C (as opposed to C++), then get a better
compiler in order to get rid of the error message.  In C this code is
legal, and we are in comp.lang.c, aren't we?

As far as decent code-writing is concerned I feel compelled to say the
following:

Whenever I see a cast in C code, then this signals: ``Watch out!
Something bogus is going on here.''  Mallocing some space, however, is
not bogus by any measure.  Therefore, ``for the sake of decent code
writing'' I avoid to use an explicit cast here.  malloc() returns
void*, which is compatible with any non-function pointer in C.  No
ANSI-compliant C compiler is allowed to reject the above code for that
reason.  (I think Jos Horsmeier made this clear already...)

Not using an explicit cast here also prevents people from compiling my
code with a C++ compiler, which must be called a *major* advantage...
(No, no smileys for that. -- At least the chances my computer doesn't
instantly turn into a pumpkin are somewhat better. -- Ok, this is
worth a :)

--
-Matthias



Sat, 21 Sep 1996 02:04:24 GMT  
 Why won't this code compile?


Quote:

>>I am trying my hand at dynamic allocation for arrays.  I checked in Mastering Borland C++ (Swan, 1992) on pages 294-295 (Dynamic Arrays) and tried the following code but can't get it to compile:

><most of the code deleted for space conservation.>

[...most of Charles's response deleted for space conservation...]

I'd like to add, Dan, that you should add <alloc.h> to your #include
list. It contains stuff you'll need for playing with dynamic allocation.
--
----
Eric

"If anyone finds this offensive, I am prepared not only to retract my
words, but also to deny under oath that I ever said them." -- Tom Lehrer
----



Sat, 21 Sep 1996 07:24:37 GMT  
 Why won't this code compile?

Quote:




>>>I am trying my hand at dynamic allocation for arrays.  I checked in Mastering Borland C++ (Swan, 1992) on pages 294-295 (Dynamic Arrays) and tried the following code but can't get it to compile:
>><most of the code deleted for space conservation.>
>[...most of Charles's response deleted for space conservation...]
>I'd like to add, Dan, that you should add <alloc.h> to your #include
>list. It contains stuff you'll need for playing with dynamic allocation.
>--
>Eric

I would like to add that on our Sun workstation (using cc) you don't need
to include alloc.h .
But on my C compiler at home I do need to include alloc.h. Therefore I always
include it for portability...

--






Sat, 21 Sep 1996 18:59:44 GMT  
 Why won't this code compile?

  .
  .

Quote:
>That depends on what you mean by "portability" -- *I* think it means
>usinf Standard C, and that means there is no such header as alloc.h ...

Agreed - the standard header file for malloc is stdlib.h. Of course that
may not be supported by some pre-ANSI compilers at which point it is probably
time to dust down those preprocessor conditionals! :-)

--
-----------------------------------------


-----------------------------------------



Mon, 30 Sep 1996 19:48:30 GMT  
 Why won't this code compile?

Quote:

>Not using an explicit cast here also prevents people from compiling my
>code with a C++ compiler, which must be called a *major* advantage...

I couldn't agree more ...

--

681 Park Street, Brunswick, Vic. 3056, Australia



Tue, 01 Oct 1996 15:20:25 GMT  
 Why won't this code compile?

Quote:

>I would like to add that on our Sun workstation (using cc) you don't need
>to include alloc.h .
>But on my C compiler at home I do need to include alloc.h. Therefore I always
>include it for portability...

That depends on what you mean by "portability" -- *I* think it means
usinf Standard C, and that means there is no such header as alloc.h ...

--

681 Park Street, Brunswick, Vic. 3056, Australia



Tue, 01 Oct 1996 15:22:06 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. MSoft's sample STL code won't compile

2. Why won't this compile?

3. (newbie)why won't the following compile??

4. Why won't this compile????

5. Why won't VC compile this

6. Helpppppp Why won't this compile?

7. why won't CoInitialize compile?

8. Why won't it compile?!

9. Why won't this compile?

10. Code which won't compile using Borland C Compiler

11. code won't compile - strlib.h

12. simple code won't compile

 

 
Powered by phpBB® Forum Software