Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

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

I'm finally beginning to understand the Networking on Winsock. I was following this tutorial ; https://www.binarytides.com/winsock-sock...-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.
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.
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)
(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)
(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
Selling signature space - PM if interested


hi mr fbi
(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.
C++ requires you to specify const while C doesn't.
(Jul 24, 2018 04:43 AM)F.A.S.T Wrote:  Also, How do you receive an answer from the server properly.

define properly
[Image: 5Iq5rDJ.gif]

winapi, and winsock programmer



Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by maryousJul 24, 2022 03:35 PM 0 160 Jul 24, 2022 03:35 PM
Last Post: maryous
[.NET] by gijivargijiAug 10, 2021 09:06 PM 4 216 Aug 10, 2021 09:06 PM
Last Post: Shadow_Cloud
[C] by pokerJan 8, 2021 04:29 PM 0 146 Jan 8, 2021 04:29 PM
Last Post: poker