473,395 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

typecasting std::string to const void *

Hi,

This is the part of the code am trying to compile to :

void Server::respondToClient ( std::string response )
{
....
....
if ((numbytes = sendto ( sockFd_ , response , sizeof(response) , 0,
(struct sockaddr *)&clientAddr, sizeof (clientAddr))) == -1){
perror ("sendto");
exit (1) ;
}
....
}

This is the function prototype :
ssize_t sendto(int socket, const void *message, size_t length,
int flags, const struct sockaddr *dest_addr,
socklen_t dest_len);

Not sure how to go about typecasting the 'std::string' to 'const void
*' .

Thanks in advance ,
vivekian

Mar 21 '06 #1
4 7719
TB
vivekian skrev:
Hi,

This is the part of the code am trying to compile to :

void Server::respondToClient ( std::string response )
{
...
...
if ((numbytes = sendto ( sockFd_ , response , sizeof(response) , 0,
if ((sendto( sockFd_, response.c_str(), response.size(), 0,
(struct sockaddr *)&clientAddr, sizeof (clientAddr))) == -1){
perror ("sendto");
exit (1) ;
}
....
}

This is the function prototype :
ssize_t sendto(int socket, const void *message, size_t length,
int flags, const struct sockaddr *dest_addr,
socklen_t dest_len);

Not sure how to go about typecasting the 'std::string' to 'const void
*' .

Thanks in advance ,
vivekian


--
TB @ SWEDEN
Mar 21 '06 #2
vivekian <vi********@gmail.com> wrote:
This is the part of the code am trying to compile to :

void Server::respondToClient ( std::string response )
You might want to consider passing a const reference to avoid
unnecessary copying:

void Server::respondToClient (std::string const& response)
{
...
...
if ((numbytes = sendto ( sockFd_ , response , sizeof(response) , 0,
(struct sockaddr *)&clientAddr, sizeof (clientAddr))) == -1){
perror ("sendto");
exit (1) ;
}
....
}

This is the function prototype :
ssize_t sendto(int socket, const void *message, size_t length,
int flags, const struct sockaddr *dest_addr,
socklen_t dest_len);

Not sure how to go about typecasting the 'std::string' to 'const void
*' .


You do not. You pass the data of the string and the length:

sendto (sockFd_, response.data (), response.length (), ...

hth
--
jb

(reply address in rot13, unscramble first)
Mar 21 '06 #3

vivekian wrote:
Hi,

This is the part of the code am trying to compile to :

void Server::respondToClient ( std::string response )
{
...
...
if ((numbytes = sendto ( sockFd_ , response , sizeof(response) , 0,
(struct sockaddr *)&clientAddr, sizeof (clientAddr))) == -1){
perror ("sendto");
exit (1) ;
}
....
}

This is the function prototype :
ssize_t sendto(int socket, const void *message, size_t length,
int flags, const struct sockaddr *dest_addr,
socklen_t dest_len);

Not sure how to go about typecasting the 'std::string' to 'const void
*' .


To add a general point to the answers you've already got: the compiler
is your friend. Typecasting is not for situations where you are not
sure how to go about something. Using a cast amounts to silencing the
compiler and overruling its understanding of the language rules.
Therefore, you should only cast when you know exactly what you are
doing and why, in that particular case, you know better than the
compiler.

Gavin Deane

Mar 21 '06 #4

Jakob Bieling wrote:
vivekian <vi********@gmail.com> wrote:
This is the part of the code am trying to compile to :

void Server::respondToClient ( std::string response )


You might want to consider passing a const reference to avoid
unnecessary copying:

void Server::respondToClient (std::string const& response)
{
...
...
if ((numbytes = sendto ( sockFd_ , response , sizeof(response) , 0,
(struct sockaddr *)&clientAddr, sizeof (clientAddr))) == -1){
perror ("sendto");
exit (1) ;
}
....
}

This is the function prototype :
ssize_t sendto(int socket, const void *message, size_t length,
int flags, const struct sockaddr *dest_addr,
socklen_t dest_len);

Not sure how to go about typecasting the 'std::string' to 'const void
*' .


You do not. You pass the data of the string and the length:

sendto (sockFd_, response.data (), response.length (), ...


This works :). But when passing a const reference , end up getting a
segmentation fault. After some debugging it seems , it occurs during '
response.length()' . Though the program works by pass by value , would
still like to discover the cause of the segmentation fault.

thanks .

Mar 22 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while(...
22
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; ...
6
by: Nemok | last post by:
Hi, I am new to STD so I have some questions about std::string because I want use it in one of my projects instead of CString. 1. Is memory set dinamicaly (like CString), can I define for...
2
by: anelma via .NET 247 | last post by:
Following code works fine, when compiled with VS 6.0, but not anymore when compiled in .NET. What's wrong here, I can't see it by myself? arrString content will be garbage with .net compilation, but...
12
by: Vincent RICHOMME | last post by:
Hi, I am currently implementing some basic classes from .NET into modern C++. And I would like to know if someone would know a non mutable string class.
10
by: sposes | last post by:
Im very much a newbie but perhaps somehone can help me. Ive been searching for a way to convert a std::string to a unsigned char* The situation is I have a function that wants a unsigned char*...
84
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
11
by: Jacek Dziedzic | last post by:
Hi! I need a routine like: std::string nth_word(const std::string &s, unsigned int n) { // return n-th word from the string, n is 0-based // if 's' contains too few words, return "" //...
11
by: tech | last post by:
Hi, I need a function to specify a match pattern including using wildcard characters as below to find chars in a std::string. The match pattern can contain the wildcard characters "*" and "?",...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.