How to refuse the warning "conversion to" char from "int can change its value")

I though, if I cast number like this (unsigned char)32 , will be enough to fix the compiler warning, but it was not the way I planned.

Here I have the following part of the program that really explains the problem:

 #include <stdio.h> #include <string.h> #include <stdlib.h> int main(void){ char *ptr = malloc(6); const char *name = "MICHI"; unsigned int i = 0; if(ptr){ strcpy(ptr, name); ptr[strlen(ptr)] = '\0'; }else{ return 1; } while(ptr[i] != '\0'){ if((ptr[i] >= 'A') && (ptr[i] <= 'Z')){ ptr[i] += (unsigned char)32; } i++; } printf("Name = %s\n",ptr); if(ptr){ free(ptr); ptr = NULL; } } 

When I try to compile it with compiler warnings, I get the following:

 error: conversion to 'char' from 'int' may alter its value [-Werror=conversion]| 

This means that the next ptr[i] += (unsigned char)32; doesn't provide a solution to my problem.

My question is how to discard this warning because I have no idea about it.

Ideone does not help, because I think all warnings are disabled.

+1
c casting
Feb 16 '16 at 17:50
source share
4 answers

OP uses a warning level that is very picky

warning: converting to 'char' from 'int' may change its value [-Wconversion]

  // Both cause the warning ptr[i] += (unsigned char) 32; ptr[i] = tolower(ptr[i]); 

To resolve this warning, be explicit

  ptr[i] = (char) (ptr[i] + 32); ptr[i] = (char) tolower(ptr[i]); 



[Details] Operations with a narrow type of type char, short, unsigned char, _Bool, ... will have an advanced operand using ordinary integer promotions up to int/unsigned , for example ptr[i] . So assigning int/unsigned back to char raises a warning. Explicit expression of the result raises a warning.

Many compilations omit the [-Wconversion] or equivalent option and therefore will not see a warning.

+4
Feb 16 '16 at 18:31
source share

Adding to the comments, there is actually a way to silence the warning using only casts:

 ptr[i] = (char)((int)ptr[i] + 32); 

Thus, the conversion transaction is delegated to the assignment operator, which (as specified by @Olaf), defined in Standard, 6.5.16.1p3 .

+2
Feb 16 '16 at 18:33
source share

This line:

 ptr[i] += (unsigned char)32; 

really means:

 ptr[i] = ptr[i] + 32; 

Now ... ptr[i] can legally be the size of 255. And if you add 255 + 32, you get 287.

How can you set the value to 287 inside char , which has a maximum value of 255?
You just can't do it. Here is what the error is.

If you want to get rid of the error, you must first clearly understand what exactly you want, when the addition can create values ​​that exceed char .

If you are just trying to convert the letters AZ to your lower equivalents, just use the appropriate api for this: tolower




If you really are not going to use tolower , try this:

 ptr[i] = (unsigned char)(ptr[i] + 32); 

An important difference is that type-casting is a result of computation, and not in one of the operands of an operation.

+1
Feb 16 '16 at 17:55
source share

The warning is due to the fact that you can override the border of the char value.

Usually avoiding warning is not a good idea in general.

Use tolower() to make the job a cleaner way.

0
Feb 16 '16 at 17:58
source share



All Articles