0

I keep getting these warnings

I want to calculate the image size in colors in (Mo) and in black and white in (Ko) so for this I'm using a parameters that are past in a terminal command which are (image length and width)

Here is my code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main(int argc, char *argv[]) {
   
   float resultKo, resultMo;

   resultKo = ((int)argv[1] * (int)argv[2]) / (1024);
   resultMo = (((int)argv[1] * (int)argv[2])/(1024 * 1024))*3;

   printf("la taille de l'image en niveau de gris : %.2fko\n",resultKo);
   printf("la taille de l'image en couleur : %.2fMo", resultMo);
   return 0;
}

  • `argv[1]` has type `char *`. There is no reason for you to cast that pointer to `int`. That won't convert the string to an integer. It will simply cast (and probably truncate) the value of the pointer itself. – Tom Karzes Dec 11 '22 at 15:23
  • Use the function ```atoi``` or ```strtol``` to convert numbers stored as text to an ```int``` or ```long int``` respectively. – Harith Dec 11 '22 at 15:24
  • Never use any element from `argv` unless you have checked `argc` first, to make sure the `argv` element is actually valid. – Some programmer dude Dec 11 '22 at 15:25

1 Answers1

0

To convert a char* to an int in C, you don't cast the pointer. Instead you use atoi.

frankplow
  • 502
  • 1
  • 12