Copy/Cast char[] to char* and int

char user_socket_server[40] = "192.168.0.1";
char user_socket_port[6] = "2000";

const char* socket_server;
int socket_port;

How can I copy ...

user_socket_server to socket_server
and
user_socket_port to socket_port

?

This is not an installation and troubleshooting question.. do yourself a favour and please read How to get the best out of this forum and post accordingly (including code tags and necessary documentation for your ask).. I’m moving this post to a more suitable place


You can’t copy because there is no memory allocated in the destination to receive the text, you need to define the target destination socket_server as array and then you could use strcpy() or to be on the safe side strncpy().

If you only want to point to the same memory location then you can do

socket_server = user_socket_server;

To transform the text into a number you can use atoi() or atol() or better strtol()

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.