Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Pointer as a function parameter

Reply
Thread Tools

Pointer as a function parameter

 
 
errfet
Guest
Posts: n/a
 
      09-15-2010
Hello everybody

I needed any method of conversion std::string to char*. I found some
useful library functions, and in order to make conversion i had to
write some code for example:

#include <iostream>
#include <cstring>
using namespace std;

int main(int argc, char **argv)
{
string str = "hello";

char *dst ;
dst = new char [str.size()+1];
strcpy(dst, str.c_str());
int size= (str.size()+1);

for(int i=0;i<size;i++) cout<<endl<<"char: "<<dst[i];
cout<<endl;
delete dst;
return 0;
}

In other words, i had to create new char[ ], execute strcpy()
function, everytime i wanted conversion... so i thought, why not to
put these instructions into one function?
I`ve writed some code :

#include <iostream>
#include <cstring>

using namespace std;

int str_to_char (char * dst, const string &str){
dst = new char [str.size()+1];
strcpy(dst, str.c_str());
return (str.size()+1);
}

int main(int argc, char **argv)
{
string str = "hello";

char *dst ;

int size = str_to_char(dst,str);

for(int i=0;i<size;i++) cout<<endl<<"char: "<<dst[i];
cout<<endl;
delete dst;
return 0;
}

It stopped working. Program ends with memory acces violation.

Could anyone tell me WHY? I guess it`s related to char * dst.

Cheers
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      09-15-2010
On 09/16/10 10:09 AM, errfet wrote:
>
> int str_to_char (char * dst, const string&str){
> dst = new char [str.size()+1];
> strcpy(dst, str.c_str());
> return (str.size()+1);
> }


White space is free these days...

Within the context of str_to_char, dst is a local variable so your
assignment is not visible outside the function. Pass it by reference.

int str_to_char (char*& dst, const string& str)


--
Ian Collins
 
Reply With Quote
 
 
 
 
Stuart Golodetz
Guest
Posts: n/a
 
      09-15-2010
errfet wrote:
> Hello everybody
>
> I needed any method of conversion std::string to char*. I found some
> useful library functions, and in order to make conversion i had to
> write some code for example:
>
> #include <iostream>
> #include <cstring>
> using namespace std;
>
> int main(int argc, char **argv)
> {
> string str = "hello";
>
> char *dst ;
> dst = new char [str.size()+1];
> strcpy(dst, str.c_str());
> int size= (str.size()+1);
>
> for(int i=0;i<size;i++) cout<<endl<<"char: "<<dst[i];
> cout<<endl;
> delete dst;
> return 0;
> }
>
> In other words, i had to create new char[ ], execute strcpy()
> function, everytime i wanted conversion... so i thought, why not to
> put these instructions into one function?
> I`ve writed some code :
>
> #include <iostream>
> #include <cstring>
>
> using namespace std;
>
> int str_to_char (char * dst, const string &str){
> dst = new char [str.size()+1];
> strcpy(dst, str.c_str());
> return (str.size()+1);
> }
>
> int main(int argc, char **argv)
> {
> string str = "hello";
>
> char *dst ;
>
> int size = str_to_char(dst,str);
>
> for(int i=0;i<size;i++) cout<<endl<<"char: "<<dst[i];
> cout<<endl;
> delete dst;
> return 0;
> }
>
> It stopped working. Program ends with memory acces violation.
>
> Could anyone tell me WHY? I guess it`s related to char * dst.
>
> Cheers


(Context: I just replied to the same message in alt.comp.lang.learn.c-c++)

Just FYI, multi-posting (i.e. posting the same message to separate
newsgroups one at a time) isn't such a great plan -- if you want to post
to several newsgroups (only where the message is appropriate in all the
groups and it's really sensible), then cross-posting (posting the same
thing to separate newsgroups in a single message) is the way to go.

Cheers!
Stu
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      09-16-2010
On Sep 15, 3:09*pm, errfet <(E-Mail Removed)> wrote:
> Hello everybody
>
> I needed any method of conversion std::string to char*. I found some
> useful library functions, and in order to make conversion i had to
> write some code for example:
>


First of all, note that <cstring> in <string.h> in the std::namespace.
std::string is defined in <string>

You can use a vector instead of using new[].
vector is guaranteed to be contiguous as of C++03

#include <iostream>
#include <ostream>
#include <vector>
#include <string>
using namespace std;

int main(int argc, char **argv)
{
* * * * string str = "hello";

vector<char> v(str.begin(), str.end);
v.push_back('\0');

cout << &v[0] << endl;

return 0;
}

 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      09-16-2010
On 9/15/2010 8:40 PM, red floyd wrote:
> On Sep 15, 3:09 pm, errfet<(E-Mail Removed)> wrote:
>> Hello everybody
>>
>> I needed any method of conversion std::string to char*. I found some
>> useful library functions, and in order to make conversion i had to
>> write some code for example:
>>

>
> First of all, note that<cstring> in<string.h> in the std::namespace.

Oops. <cstring> *is* <string.h>
> std::string is defined in<string>
>
> You can use a vector instead of using new[].
> vector is guaranteed to be contiguous as of C++03
>
> #include<iostream>
> #include<ostream>
> #include<vector>
> #include<string>
> using namespace std;
>
> int main(int argc, char **argv)
> {
> string str = "hello";
>
> vector<char> v(str.begin(), str.end);

Oops. str.end()


> v.push_back('\0');
>
> cout<< &v[0]<< endl;
>
> return 0;
> }
>


 
Reply With Quote
 
Helge Kruse
Guest
Posts: n/a
 
      09-16-2010

"Ian Collins" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 09/16/10 10:09 AM, errfet wrote:
>>
>> int str_to_char (char * dst, const string&str){
>> dst = new char [str.size()+1];
>> strcpy(dst, str.c_str());
>> return (str.size()+1);
>> }

>
> White space is free these days...


There is a lot of whitespace in the message. Unfortunately Outlook Express
does not display this correctly and removes it when you reply. What's new
for me was that even your Thunderbird has this problem...

> User-Agent: Mozilla/5.0 (X11; U; SunOS i86pc; en-US; rv:1.9.1.7)
> Gecko/20100214 Lightning/1.0b1 Thunderbird/3.0.1


Helge


 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      09-16-2010
"Helge Kruse" <(E-Mail Removed)> wrote in message
news:4c921f9b$0$7657$(E-Mail Removed)-online.net...

> There is a lot of whitespace in the message. Unfortunately Outlook Express
> does not display this correctly and removes it when you reply.


Not in general. That seems to be a problem when tabs are used for
formatting. That's usually a bad idea anyway, particularly on usenet.




Brian
--
Day 589 of the "no grouchy usenet posts" project.


 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      09-16-2010
"errfet" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> I`ve writed some code :


<pedantic English lesson of the day>
"I have written" or "I wrote".
</pedantic English lesson of the day>

> int str_to_char (char * dst, const string &str){
> dst = new char [str.size()+1];


This problem is covered in the C FAQ. I'm not sure if it is in the C++ FAQ
or not.

http://c-faq.com/ptrs/passptrinit.html




Brian
--
Day 589 of the "no grouchy usenet posts" project.


 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      09-16-2010
On 09/17/10 01:46 AM, Helge Kruse wrote:
> "Ian Collins"<(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> On 09/16/10 10:09 AM, errfet wrote:
>>>
>>> int str_to_char (char * dst, const string&str){
>>> dst = new char [str.size()+1];
>>> strcpy(dst, str.c_str());
>>> return (str.size()+1);
>>> }

>>
>> White space is free these days...

>
> There is a lot of whitespace in the message. Unfortunately Outlook Express
> does not display this correctly and removes it when you reply. What's new
> for me was that even your Thunderbird has this problem...


That was a recent "enhancement"! The solution (for both?) is to avoid tabs.

--
Ian Collins
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      09-16-2010
On 09/17/10 07:57 AM, Ian Collins wrote:
> On 09/17/10 01:46 AM, Helge Kruse wrote:
>> "Ian Collins"<(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> On 09/16/10 10:09 AM, errfet wrote:
>>>>
>>>> int str_to_char (char * dst, const string&str){
>>>> dst = new char [str.size()+1];
>>>> strcpy(dst, str.c_str());
>>>> return (str.size()+1);
>>>> }
>>>
>>> White space is free these days...

>>
>> There is a lot of whitespace in the message. Unfortunately Outlook
>> Express
>> does not display this correctly and removes it when you reply. What's new
>> for me was that even your Thunderbird has this problem...

>
> That was a recent "enhancement"! The solution (for both?) is to avoid tabs.


I posted too soon, spaces in the original are preserved by Thunderbird,
it was this single function that didn't have any in the OP.

--
Ian Collins
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to pass a parameter for a function parameter in a function AzamSharp Javascript 2 07-05-2008 12:24 AM
Function pointer array as parameter to a function aruna.mysore@gmail.com C Programming 25 05-20-2008 11:08 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
function pointer and member function pointer question glen stark C++ 2 10-10-2003 01:41 PM
Passing a pointer to member function as a parameter to another member function Newsgroup - Ann C++ 5 07-30-2003 02:54 AM



Advertisments