Hack Forums
[C] a value of type "const char *" cannot be used to initialize an entity of type "char * - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C] a value of type "const char *" cannot be used to initialize an entity of type "char * (/showthread.php?tid=5865326)



a value of type "const char *" cannot be used to initialize an entity of type "char * - F.A.S.T - Jul 23, 2018

I'm finally beginning to understand the Networking on Winsock. I was following this tutorial ; https://www.binarytides.com/winsock-socket-programming-tutorial/
But the way I'm doing is taking on reference and writing my own version of it.

I've came across a problem.

a value of type "const char " cannot be used to initialize an entity of type "char

Error Line :
PHP Code

<?php 
char
* msg = "GET / HTTP/1.1\r\n\r\n"; 

And this is the part where that Variable is being used :
PHP Code

<?php 
// Send Data
 
cout << "Sending Get request.." << endl;

 if (
send(sock, msg, strlen(msg), 0) < 0) {
 
cout << "Failed to Send.. " << endl;
 
getchar();
 return 
1;
 }
 
cout << "Request Sent yo!" << endl;
 
getchar();
 return 
0; 

Other Information :
- IDE : Visual Studio Community.
- OS : Windows 10.


RE: a value of type "const char *" cannot be used to initialize an entity of type "char * - 3DES - Jul 23, 2018

change
Code
char* msg = "GET / HTTP/1.1\r\n\r\n";
to
Code
const char* msg = "GET / HTTP/1.1\r\n\r\n";

const means the variable is not going to get modified during the execution of program, it will be read only.


RE: a value of type "const char *" cannot be used to initialize an entity of type "char * - vmcall - Jul 23, 2018

Because the variable is const (constant at after initialization, but unlike constexpr doesn't have to be compile-time constant), trying to pass it to a function that takes a non-const variable would essentially remove said constant protection.

You can either call const_cast<char*>(var) to remove the respective const modifier, or simply define the original value as const char* ('auto' would do that)


RE: a value of type "const char *" cannot be used to initialize an entity of type "char * - F.A.S.T - Jul 23, 2018

(Jul 23, 2018 11:46 AM)3DES Wrote:  change
Code
char* msg = "GET / HTTP/1.1\r\n\r\n";
to
Code
const char* msg = "GET / HTTP/1.1\r\n\r\n";

const means the variable is not going to get modified during the execution of program, it will be read only.
Dang I already tried that when the Error Occurred. It does not send the data then.

[Image: J1gLfRi.png]

FULL CODE (Click to View)


RE: a value of type "const char *" cannot be used to initialize an entity of type "char * - Mr. Kim - Jul 23, 2018

(Jul 23, 2018 01:14 PM)F.A.S.T Wrote:  Dang I already tried that when the Error Occurred. It does not send the data then.

[Image: J1gLfRi.png]

FULL CODE (Click to View)


Change this
Code
if (connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0) {
cout << "Connection Failure" << endl;
}

To

Code
if (connect(sock, (struct sockaddr*)&server, sizeof(server)) != SOCK_ERROR) {
cout << "Connection Failure" << endl;
}

and call WSAGetLastError() to get the actual error


RE: a value of type "const char *" cannot be used to initialize an entity of type "char * - F.A.S.T - Jul 24, 2018

(Jul 23, 2018 04:35 PM)Mr. Kim Wrote:  Change this
Code
if (connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0) {
cout << "Connection Failure" << endl;
}

To

Code
if (connect(sock, (struct sockaddr*)&server, sizeof(server)) != SOCK_ERROR) {
cout << "Connection Failure" << endl;
}

and call WSAGetLastError() to get the actual error
Nevermind thanks alot I fixed it :) (Click to View)


Also, How do you receive an answer from the server properly.


RE: a value of type "const char *" cannot be used to initialize an entity of type "char * - Hoodie+ - Jul 24, 2018

C++ requires you to specify const while C doesn't.


RE: a value of type "const char *" cannot be used to initialize an entity of type "char * - aeiro - Jul 24, 2018

(Jul 24, 2018 04:43 AM)F.A.S.T Wrote:  Also, How do you receive an answer from the server properly.

define properly