0

When I try to run the following code, I get this error:

'initializing': cannot convert from 'void *' to 'int *' 

I'd like to return an array of casual numbers from 1 to 10 from the function but I can't figure out how to do it, can you help me? The IDE underlines 'malloc'.

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

int* casuali(int n);

int main()
{
    int n = 10;

    int* arrCasuali = casuali(n);

    for (int i = 0; i < n; i++)
    {
        printf("%d", arrCasuali[i]);
    }
}

int* casuali(int n)
{
    int* arrCasuali = malloc(sizeof(int) * n);
    srand(time(NULL));

    for (int i = 0; i < n; i++)
    {
        arrCasuali[i] = rand() % 10 + 1;
    }

    return arrCasuali;
}
user3386109
  • 34,287
  • 7
  • 49
  • 68
Frax
  • 1
  • 1
    What compiler? Apart from `conio.h`, this code compiles fine for me under `clang`. – tadman Feb 01 '21 at 21:06
  • 4
    Tip: Seed *once* in your code. Don't seed per call. – tadman Feb 01 '21 at 21:06
  • I tried it on visual studio 2017 and Dev c++ but it doesn't work in both. – Frax Feb 01 '21 at 21:07
  • 4
    [You shouldn't need to cast the result of `malloc()` in C](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). Are you compiling in C++ mode inadvertently? Different rules apply there and the cast is necessary, but that is probably a mistake. Compile as C. – tadman Feb 01 '21 at 21:07
  • 6
    Your code is valid **C** and your question is tagged with **C** - but the error you mentioned will only be generated if you are compiling your code as **C++**. Check your compiler settings. – Adrian Mole Feb 01 '21 at 21:08
  • try adding `return 0;` at the end of your main function. You've told the compiler that it returns an int, but you're not returning anything. – bruceg Feb 01 '21 at 21:10
  • The issue was I was compiling in c++ mode, the code works fine now, thanks a lot. – Frax Feb 01 '21 at 21:12
  • 2
    @bruceg: `main` is special and does not need an explicit `return` statement. If program control reaches the end of `main`, it will return success. – Eric Postpischil Feb 01 '21 at 21:15
  • @EricPostpischil I was just guessing that the OP was doing something weird with a C++ compiler that needed the return – bruceg Feb 01 '21 at 21:38
  • do not compile as C++ – 0___________ Feb 01 '21 at 22:01

1 Answers1

2

You are compiling the program as a C++ program. To make the program to compile either compile it as a C program or at least change this declaration

int* arrCasuali = malloc(sizeof(int) * n);

to the following

int* arrCasuali = ( int * )malloc(sizeof(int) * n);

Pay attention to that among these headers

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

you need only three headers

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

or if you are compiling the program as a C++ program then

#include <cstdio>
#include <cstdlib>
#include <ctime>

and these two headers

#include <memory.h>
#include <conio.h>

are not standard C headers.

And you should free the allocated memory

free(  arrCasuali );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335