gcc task.c task.c: In function ‘sorted_str’: task.c:57:57: warning: assignment to ‘char’…
Rate quality
Private research annotation
Sign in to add a private research label or note.
- User
gcc task.c task.c: In function ‘sorted_str’: task.c:57:57: warning: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion] 57 | *(sorted_str+j) = "К"; | ^ task.c:59:55: warning: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion] 59 | sorted_str[j] = "В"; |
- Assistant
The warnings you're encountering are due to assigning a string literal (which is essentially a pointer to the string data) to a
chartype variable. In C, acharvariable can only hold a single character, while a string literal, such as"К"or"В", is a pointer to the entire string (even if it's just one character).To resolve this issue, you should assign a single character to the
charvariable, not a string. You can do this by using single quotes instead of double quotes. Single quotes denote a character literal, which represents a single character value.Here's how you can modify your code to fix these warnings:
c*(sorted_str + j) = 'К'; // Use single quotes for character literalscsorted_str[j] = 'В'; // Use single quotes for character literalsThese changes will ensure that you are assigning a single character value to the
charvariable, which aligns with the expected data type. - User
└─# gcc task.c task.c: In function ‘sorted_str’: task.c:57:57: warning: multi-character character constant [-Wmultichar] 57 | sorted_str[j] = 'К'; | ^~~ task.c:57:57: warning: overflow in conversion from ‘int’ to ‘char’ changes value from ‘53402’ to ‘-102’ [-Woverflow] task.c:59:57: warning: multi-character character constant [-Wmultichar] 59 | sorted_str[j] = 'В'; | ^~~ task.c:59:57: warning: overflow in conversion from ‘int’ to ‘char’ changes value from ‘53394’ to ‘-110’ [-Woverflow]
- Assistant
The warnings you're seeing are due to the use of multi-character character constants. In C, a character constant like
'К'is supposed to represent a single character, but because'К'is a Unicode character that doesn't fit into a singlechar(which is a single byte in C), the compiler is interpreting it as a multi-character constant, which is not what you want.To handle Unicode characters properly in C, you need to consider character encoding:
-
Ensure Your Source Files are Saved with UTF-8 Encoding: Make sure that your source file,
task.c, is saved using UTF-8 encoding.
-