-1

Below is my code segment for a homework assignment in my class, but when I try to compile it pops:

[Error] invalid conversion from 'void*' to 'int*' [-fpermissive]

on the pointers1-3, the choice, and the two options. I am still a newbie programmer and I am not sure why that is occurring now.

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

int main() 
{
    int *pointer1, *pointer2, *pointer3;
    int *choice;

    char * option;
    char * option1;

    pointer1 = malloc ( sizeof(int) );
    pointer2 = malloc ( sizeof(int) );
    pointer3 = malloc ( sizeof(int) );
    choice   = malloc ( sizeof(int) );
    option = malloc ( sizeof(char) );
    option1 = malloc ( sizeof(char) );
}
Clifford
  • 76,825
  • 12
  • 79
  • 145
  • 11
    It looks like you are trying to compile C code as C++ code. The languages share some similarities but they are distinct languages. – François Andrieux Oct 03 '19 at 20:33
  • How are you compiling the code, and what is the name of the source file? – dbush Oct 03 '19 at 20:33
  • 3
    In C you don't need a type cast when assigning from `void*` to another pointer type. In C++ you do have to cast. – Barmar Oct 03 '19 at 20:34
  • 2
    `malloc()` returns a `void*`. In C, a `void*` can be assigned to another pointer type and the compiler will convert it implicitly. But in C++, that is not the case, the cast must be done explicitly instead. – Remy Lebeau Oct 03 '19 at 20:35
  • Wow what a simple error, I changed the file type and it worked, thank you guys. – Safer Mundo Chioma Oct 03 '19 at 20:37
  • Voted to close as unclear since it tags both C and C++ but asks about an issue which is different between them. Questions like this need to be clear for future readers. – Eric Postpischil Oct 03 '19 at 20:51
  • If you are programming in C++, you should use `operator new`. – Thomas Matthews Oct 04 '19 at 00:25

1 Answers1

-3

Malloc function will return a void*. You need to cast it to the correct type like so:

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


int main()
{
    int* pointer1, * pointer2, * pointer3;
    int* choice;
    char* option;
    char* option1;

    pointer1 = (int*) malloc(sizeof(int));

    pointer2 = (int*) malloc(sizeof(int));

    pointer3 = (int*) malloc(sizeof(int));

    choice = (int*) malloc(sizeof(int));

    option = (char*) malloc(sizeof(char));

    option1 = (char*) malloc(sizeof(char));

    // Clean up after yourself!!!
    free(pointer1);
    free(pointer2);
    free(pointer3);
    free(choice);
    free(option);
    free(option1);

    return 0;
}



And also, do not forget to free up used memory!

SkyZip
  • 127
  • 1
  • 9
  • 5
    This is the case for `c++`, but not `c`. And if you are programming in `c++`, you should most likely not use `malloc` (or `new` for that matter) – HAL9000 Oct 03 '19 at 20:45
  • 1
    Recommended reading: [Why should C++ programmers minimize use of 'new'?](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new) – user4581301 Oct 03 '19 at 20:57
  • I get your point. But at the same time I just wanted to help the OP solve his error message. Not just telling him casually: 'This is not how you do it!' – SkyZip Oct 03 '19 at 21:07