I first thought that your teacher was complaining that c/caractere was defined as a pointer but treated like a char. (It only works for technical implementation reasons.)
I have no idea why your teacher thinks that a single int can be substituted for an array of int.
When declaring a character, make it a character and treat it like a character.
1 2 3
char c;
scanf( "%c", &c );
printf( "%c\n", c );
When declaring an array, make it an array and treat it like an array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int* xs;
int n;
int i;
scanf( "%d", &n );
if (n <= 0) return 1;
xs = (int*)malloc( n * sizeof(int) );
for (i = 0; i < n; i++)
scanf( "%d", &(xs[i]) );
for (i = 0; i < n; i++)
printf( "%d ", xs[i] );
free( xs );
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
etc
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:
1 2
char c;
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.
All that is not the real issue (although an incompetent teacher is). This is:
You write:
1 2
char* c;
scanf("%c", &c);
Duthomas writes:
1 2
char c;
scanf( "%c", &c );
A char* is not a char. It is a pointer. Pointers store addresses. One does not write a char into a pointer.
Do you see what is different between your and Duthomas' versions?
Do you comprehend how huge the difference is?
Does your teacher understand the significance?
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.