Invalid conversion from "const char *" to "char"

I wanted to post this because I was not sure if I had a problem with a simple assignment operation. I am doing my homework, which asks me to write structures and functions in a simple program to draw shapes of ASCII characters. Now I'm just trying to check the functions I wrote, and I'm trying to assign a value to the symbol element of the Circle structure in order to check the DrawShape function that I wrote. When I try to assign it * char, I get the error message "error: invalid conversion from" const char * 'to' char '". I will put all the code, although it is very long and incomplete. Any help with this would be appreciated The problem I get is right at the beginning of main on "circle1.char = '*'"

#include <iostream> #include <math.h> #include <cstdlib> using namespace std; const int NUMBER_OF_ROWS = 26; const int NUMBER_OF_COLUMNS = 81; char drawSpace[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]; struct Point{ int x; int y; }; struct Circle{ Point center; int radius; char symbol; bool buffer[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]; }; bool setCircleRadius(Circle &b, int r); bool setCircleCenter(Circle &b, int x, int y); bool moveCircle(Circle &b, int x, int y); void drawCircle (Circle b); void lineChars(Line a); void circleChars(Circle b); void drawShapes(); int main() { Circle circle1; circle1.radius = 5; circle1.symbol = "*"; circle1.center.x = 40; circle1.center.y = 10; drawCircle(circle1); return 0; } 
+6
source share
7 answers

You must use single quotes for characters. Double quotation marks mean that you are using a (potentially single-character) string literal, which is represented as const char * (pointer to a constant character).

The correct syntax is: circle1.symbol = '*';

+18
source

The problem is here:

 circle1.symbol = "*"; 

circle1.symbol defined as char , but you assign it a string (an array of characters). What you need to do is

 circle1.symbol = '*'; 
+5
source

Your Circle definition states that symbol is char , but you are trying to assign it a string literal of type char[2] :

 circle1.symbol = "*"; 

Instead, you should assign it a char :

 circle1.symbol = '*'; 
+1
source

You have:

 circle1.symbol = "*"; 

You need:

 circle1.symbol = '*'; 
+1
source

In C ++, one char is not written with double quites, but with single quotes, i.e. '*' , not "*" . In fact, “*” is an array of two characters, the first of which is '*' and the second is '\0' to mark the end of the line.

0
source

The "character" member of your Circle structure is defined as a single char. Although it looks like you are assigning char, you are actually assigning a string or char * of length 1. Difference: char a = 'a'; char * a = "a"; All this is in quotation marks.

0
source

Your error is on the line circle1.symbol = "*"; . "*" is const char * symbol (of your structure) is char Try: circle1.symbol = '*';

0
source

Source: https://habr.com/ru/post/901658/


All Articles