In function 'int main()':
16:34: error: cast from 'void*' to 'int' loses precision [-fpermissive]
16:5: error: invalid conversion from 'int' to 'int*' [-fpermissive]
33:16: warning: format '%c' expects argument of type 'char*', but argument 2 has type 'char**' [-Wformat=]
Note how it says that malloc() returns a pointer, which you explicitly try to convert into an int and then store into pointer variable, invoking an implicit (attempt of) conversion from int to pointer.
The "teacher code" get more:
In function 'int main()':
17:40: error: cast from 'void*' to 'int' loses precision [-fpermissive]
23:23: error: invalid types 'int[int]' for array subscript
29:18: error: invalid types 'int[int]' for array subscript
35:12: error: invalid conversion from 'int' to 'void*' [-fpermissive]
36:8: warning: converting to non-pointer type 'int' from NULL [-Wconversion-null]
38:24: warning: format '%c' expects argument of type 'char*', but argument 2 has type 'char**' [-Wformat=]
39:24: warning: format '%c' expects argument of type 'char*', but argument 2 has type 'char**' [-Wformat=]
Your C compiler should have an option to be similarly verbose.
Make it verbose and learn to read what it says.
You changed your first post, invalidating my answer... which you have not read anyway, it seems.
char* != char int* != int
What you are doing is undefined behavior, or, more bluntly, Just Plain Wrong™.
The first thing you must learn to do, particularly in C, is learn to pay attention to the type of things. The best way is to crank up your compiler warnings to maximum, then fix the errors and warnings.
To scan a single, non-whitespace character, use:
scanf( "%c", &c );
Gee, this looks identical to the example I already posted for you!
Sorry if you take this too harshly. I’m in a rotten mood. But what I type is still correct. Learn or not.
Maybe the teacher is incompetent or maybe their words haven’t been reported correctly :-(
It’s possible the teacher was trying to warn the OP that using a not correctly initialised pointer leads to undefined behaviour, which by chance sometimes could be the expected behaviour.
So, if we have a piece of code that apparently behaves correctly, but a teacher (or in general a more experienced programmer) tell us it’s wrong, we should not just stick to the fact our code (apparently) works, but try to understand what’s the issue.
God bless you for being so kind to me.
I was just trying to get some light. That the code may or may not work , just that. And you undestood. Others post was somewhat offensive.
And yes, i know what a pointer is. Maybe was the compiler getting garbage after so many compilations i dunno.