473,785 Members | 2,395 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

typecasting std::string to const void *

Hi,

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

void Server::respond ToClient ( 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 7746
TB
vivekian skrev:
Hi,

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

void Server::respond ToClient ( 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********@gma il.com> wrote:
This is the part of the code am trying to compile to :

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

void Server::respond ToClient (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::respond ToClient ( 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********@gma il.com> wrote:
This is the part of the code am trying to compile to :

void Server::respond ToClient ( std::string response )


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

void Server::respond ToClient (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
3662
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( (idx=str.find_first_of(',')) >= 0 ) { str.replace( idx, 1, "" ); str.insert( idx, " or " ); }
22
13334
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; char c; std::string str;
6
11510
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 example string str1; as a class member and then add text to it. or do I have to specify it's length when defining? 2. How to convert from std::string to LPCSTR
2
1940
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 when compiled with 6.0 it contains string from Vector (that's how I want it to work). std::vector<std::string> Vector; ... void MyClass::DoThis(std::vector<std::string> Vector) { const char *arrString;
12
3153
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
26647
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* and I want to give it a std::string no matching function for call to `MD5::update(std::string&, size_t)' candidates are: void MD5::update(unsigned char*, unsigned int) void PrintMD5(string str){
84
15905
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
2904
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 "" // 'words' are any sequences of non-whitespace characters // leading, trailing and multiple whitespace characters // should be ignored.
11
4846
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 "?", where "*" matches zero or more consecutive occurrences of any character and "?" matches a single occurrence of any character. Does boost or some other library have this capability? If boost does have this, do i need to include an entire
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9485
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10098
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8986
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3662
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2890
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.