Initially, i ran this code on ubuntu and it worked just fine without any warnings whatsoever. However, when I run it on VS on windows it says _operand1
is not initialized. I'm wondering how it can go wrong.
I know about not casting results of malloc, but VS just keeps throwing warnings.
Program is supposed to take char array of 9 bytes. First byte represents arithmetic operation, and other 8 represent 2 ints 4 bytes each (4-digit numbers). Here is the code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
float* calculate(char *, int *, int *);
int main() {
char buffer[9];
gets_s(buffer);
int a, b;
float* rez = calculate(buffer, &a, &b);
printf("Operand1: %d, Operand 2: %d\n Result: %f\n", a, b, *rez);
return 0;
}
float* calculate(char *buffer, int *a, int *b) {
char operation;
char *_operand1;
char *_operand2;
int operand1, operand2;
memcpy(_operand1, buffer + 1, sizeof(int));
_operand2 = (buffer + 5);
operand1 = atoi(_operand1);
operand2 = atoi(_operand2);
operation = buffer[0];
float *rez = (float *)malloc(sizeof(float));
switch (operation) {
case '0':
*rez = (float)(operand1 + operand2);
break;
case '1':
*rez = (float)(operand1 - operand2);
break;
case '2':
*rez = (float)(operand1 * operand2);
break;
case '3':
*rez = (float)operand1 / operand2;
break;
}
return rez;
}