Strange c code, aprecciate some light

Hello, guys , this is my first post.
I'll paste an exercise code i made for my school.

This one
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "stdio.h"
#include "stdlib.h"

int main()

{
	char *c;
	int *m1;
	int n; 
	int x;
	int soma = 0;

	printf("Choose an array size");
	scanf("%d", &n);

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


	for (x = 0; x < n; x++) {
		printf("Digit the numbers #%d ", x+1);
		scanf("%d", &m1[x]);
	}

	for (x = 0; x < n; x++) {
		
		soma = m1[x] + soma;
	}

	printf("\n value adition: %d ", soma);

	free (m1);

	scanf("%c", &c);
	return 0;


See line 15 --> " m1 = (int)malloc(n * sizeof(int)); " ?

try using this -> " m1 = (int*)malloc(n * sizeof(int)); "

The result is absolutely the same.

BUT

My teacher resolution "which just vary some variables names dont work if we take out the "*".

My teacher resolution :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "stdio.h"
#include "stdlib.h"

int main()
{
	char *caractere;
	int n;
	int vetor; 
	int x;
	int soma = 0;

	
	printf("Entre com o tamanho do vetor: ");
	scanf("%d", &n);

	
	vetor = (int ) malloc (n * sizeof(int));
	
	
	for(x=0; x<n; x++) 
	{
		printf("Entre com o valor do elemento #%d:", x+1);
		scanf("%d", &vetor[x]);
	}

	
	for(x=0; x<n; x++) 
	{
		soma += vetor[x];
	}

	
	printf("Total da soma dos elementos: %d", soma);

	free(vetor);	
	vetor = NULL;	
	
	scanf("%c", &caractere); 
	scanf("%c", &caractere);
	return 0;

}

See line 16 "vetor = (int *) malloc (n * sizeof(int));"
Try using "vetor = (int ) malloc (n * sizeof(int));"

The code dont work. Anyone know why ? Sorry for the long post!
Last edited on
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 );

Hope this helps.
A (C++) compiler says this about your code:
 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.
Ty for the replies guys. My teacher told me that it may or may not work depend on the case.

This is for ...

1
2
3
4
5
6
7
8
int main()

{
	char *c;
	
	scanf("%c", &c);
	return 0;
	}


... keep the terminal open if you press enter one ( entering an printf scanf command for example). Try it out.
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.
Duthomhas, i saw your post friend.

In fact my question was another. Is was something about a pointer in the array m1. But like i said , he said that it may work sometimes.

Sorry if i changed subjects.. I just said earlier that if you use :
1
2
3
4
5
6
7
8
int main()

{
	char *c;
	
	scanf("%c", &c);
	return 0;
	}


Everytime you need system pause you can paste scanf("%c", &c);. Try it out if didnt know about this:D
he said that it may work sometimes

And a broken clock looks like it is on current time twice a day. That does not make it right.

Everytime you need system pause you can paste

Pray tell, is there a way to paste sanity into brains?

Try it out if didnt know about this:D

This Forum has two sticky threads. Only two. You saw them, did you not?
This: http://www.cplusplus.com/forum/beginner/1988/


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.
Ty very much Enoizat!

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.
Topic archived. No new replies allowed.