Passing constant pointers to functions

Hi all, I have two member function definitions below, I am having trouble as one accepts a non const pointer but const string, and the other accepts a non const pointer but a non const value. When I try to pass const char* recievedstring to readString() the compiler throws an error. I did try to look into const_cast but no luck. Alternatively I thought about modifying the header file function defition to remove the const requirement. It's probably something simple so any tips welcome! Thank you



1
2
3
4
5
6
7
8
9
10
11
12

char serialib::writeString	(	const char * 	receivedString	)




int serialib::readString	(	char * 	receivedString,
char 	finalChar,
unsigned int 	maxNbBytes,
const unsigned int 	timeOut_ms = 0 
)		
Actually you do not provide a const pointer. You provide a pointer to const char.

Is there a reason why parameter for readString(...) isn't const? Maybe receivedString will be modified? If so you cannot provide a const string.

By the way: Consider using std::string ...
Well I would imagine that receivedString points to the memory allocated for the received string and so that memory cannot be marked as const. Hence char * and not const char*. So any memory address passed to readString must be non-const marked.
Thank you, it is now solved! There was a typo in the header file...
Topic archived. No new replies allowed.