Topic: Why won't this code compile?


Author: djm134@psu.edu (Dan Mouer)
Date: 2 Apr 1994 02:55:04 GMT
Raw View

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.




Author: bobzim@interaccess.com (Bob Zimmerman)
Date: 2 Apr 1994 03:05:17 GMT
Raw View
Dan Mouer (djm134@psu.edu) wrote:


> 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:

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

> I keep getting the following error:

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

The beauty of C++ and type checking!!! (in C this wouldn't have been a
problem)...

You have defined the RandomArray as a pointer to a "double"... but malloc
return a pointer to "void"... C++ says... you may have a problem!

Two ways around... First you can shove it in <Grin>...
RandomArray = (double*) malloc etc....

Casting it to a double * pointer tells C++ that malloc will return a
pointer to double... (even if it actually doesn't - you tell C++ consider
it done)...

The other way is to switch from malloc to "new" and from "free" to
"delete" ... (the c++ way)...

RandomArray = new double[arraysize];



 --
---------------------------------------------------
| Bob Zimmerman   bobzim@interaccess.com          |
|                                                 |
| If it's worth reading,                          |
|             you found it on Internet!           |
---------------------------------------------------




Author: lincmad@netcom.com (Linc Madison)
Date: Sat, 2 Apr 1994 22:55:54 GMT
Raw View
Dan Mouer (djm134@psu.edu) wrote:

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

[BTW, please use the return key to keep lines under 80 characters.]

: 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?

The function "malloc" returns a "pointer to void" (void *).  You have
declared RandomArray as (double *).  You cannot implicitly cast the one
to the other, as you could in C.  You must use an explicit cast; i.e.,
(double*)malloc(..).  Or, better yet, use the C++ "new" operator,
RandomArray = new double[arraysize].  You then need to use "delete"
instead of "free" to release the memory.

-- Linc Madison   *   Oakland, California   *   LincMad@Netcom.com




Author: Eric M. Berdahl <eric_berdahl@taligent.com>
Date: Sun, 3 Apr 1994 00:26:58 GMT
Raw View
In article <2nimq8$a2u@hearst.cac.psu.edu> Dan Mouer, djm134@psu.edu
writes:
> RandomArray = malloc(arraysize * sizeof(double));
> }
>
>
>I keep getting the following error:
>
> Cannot convert 'void *' to 'double *'
>
>Please, any C/C++ gurus have a clue?

Based on this excerpt, I would guess that your malloc function is
returning a void*. RandomArray is declared as a double*, so assigning a
void* to it is a compile-time error. What you'll need to do is either
change your code to not use malloc

   RandomArray = new double[arraysize];

or change your code to case the malloc result to a double*.

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

Personally, I prefer using operator new, but either path should work.

Hope this helps,
Eric
eric_berdahl@taligent.com




Author: ben@coral.cs.jcu.edu.au (Ben Cockfield)
Date: 4 Apr 94 06:25:11 GMT
Raw View
In <2nimq8$a2u@hearst.cac.psu.edu> djm134@psu.edu  (Dan Mouer) writes:



>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:


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

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

The code sent is below, but the error lies in this line. malloc returns a
pointer to void or void *, you are trying to assign this to a variable
declared as double *. Hence the error. To get around this you must convert
the returned memory to double * by using the normal C method thus:

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

or by using the C++ notation of definig a another type witha type def and
doing the converion that way:

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

Either of the methods stated above work.

/*********************************************************************/
-------original message follows ------

>*/

>#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.




-----------------------------------
| If you are sincere then blood   |         Ben Cockfield
| vanishes and fear gives way.    |       ben@cs.jcu.edu.au
-----------------------------------