Casting malloc - Cprogramming.com

> professor and all other tutors said that we should cast the memory allocation
Yeah, maybe they're closet C++ programmers.

If you discount the case of trying to write 30+ year old C code with a compiler from way before 1989, there are only two bad cases to worry about.
Neither of which are solved by casting malloc.

1. Forgetting to include stdlib.h
Code:
$ cat foo.c
#include <stdio.h>
//#include <stdlib.h>
int main()
{
    printf("sizeof int=%zd, sizeof ptr=%zd\n", sizeof(int), sizeof(char*));
    char *p = malloc(10);
    char *q = (char*)malloc(20);
    return 0;
}
$ gcc foo.c
foo.c: In function ‘main’:
foo.c:6:15: warning: implicit declaration of function ‘malloc’ [-Wimplicit-function-declaration]
    6 |     char *p = malloc(10);
      |               ^~~~~~
foo.c:2:1: note: include ‘<stdlib.h>’ or provide a declaration of ‘malloc’
    1 | #include <stdio.h>
  +++ |+#include <stdlib.h>
    2 | //#include <stdlib.h>
foo.c:6:15: warning: incompatible implicit declaration of built-in function ‘malloc’ [-Wbuiltin-declaration-mismatch]
    6 |     char *p = malloc(10);
      |               ^~~~~~
foo.c:6:15: note: include ‘<stdlib.h>’ or provide a declaration of ‘malloc’
$ ./a.out 
sizeof int=4, sizeof ptr=8
Other compilers would be less chatty, and just implicitly declare int malloc();
Without the cast, you would at least get a warning about an incompatible type assignment.
With the cast, you get nothing.
The code is thoroughly broken on machines where pointers and integers have different sizes (as is the case here).

2. Compiling C code with a C++ compiler.
This is an all too easy trap to fall into, if you let IDEs do the work of setting up a project for you (looking at you visual studio).
Code:
$ cat foo.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
    printf("sizeof int=%zd, sizeof ptr=%zd\n", sizeof(int), sizeof(char*));
    char *p = malloc(10);
    char *q = (char*)malloc(20);
    return 0;
}
$ gcc -xc++ foo.c
foo.c: In function ‘int main()’:
foo.c:6:21: error: invalid conversion from ‘void*’ to ‘char*’ [-fpermissive]
    6 |     char *p = malloc(10);
      |               ~~~~~~^~~~
      |                     |
      |                     void*
The cast only serves to make the compiler shut up, and completely hides the fact that you should be using a C compiler to compile C code.

Despite what many claim, C++ is NOT a superset (either mathematical or semantic) of C.