Pointer as a function parameter

Discussion in 'C++' started by errfet, Sep 15, 2010.

  1. errfet

    errfet Guest

    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;
    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;
    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 :)
     
    errfet, Sep 15, 2010
    #1
    1. Advertising

  2. errfet

    Ian Collins Guest

    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
     
    Ian Collins, Sep 15, 2010
    #2
    1. Advertising

  3. 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;
    > 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;
    > 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
     
    Stuart Golodetz, Sep 16, 2010
    #3
  4. errfet

    red floyd Guest

    On Sep 15, 3:09 pm, 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:
    >


    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;
    }
     
    red floyd, Sep 16, 2010
    #4
  5. errfet

    red floyd Guest

    On 9/15/2010 8:40 PM, red floyd wrote:
    > On Sep 15, 3:09 pm, 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:
    >>

    >
    > 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;
    > }
    >
     
    red floyd, Sep 16, 2010
    #5
  6. errfet

    Helge Kruse Guest

    "Ian Collins" <> wrote in message
    news:...
    > 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
     
    Helge Kruse, Sep 16, 2010
    #6
  7. errfet

    Default User Guest

    "Helge Kruse" <> wrote in message
    news:4c921f9b$0$7657$-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.
     
    Default User, Sep 16, 2010
    #7
  8. errfet

    Default User Guest

    "errfet" <> wrote in message
    news:...

    > 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.
     
    Default User, Sep 16, 2010
    #8
  9. errfet

    Ian Collins Guest

    On 09/17/10 01:46 AM, Helge Kruse wrote:
    > "Ian Collins"<> wrote in message
    > news:...
    >> 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
     
    Ian Collins, Sep 16, 2010
    #9
  10. errfet

    Ian Collins Guest

    On 09/17/10 07:57 AM, Ian Collins wrote:
    > On 09/17/10 01:46 AM, Helge Kruse wrote:
    >> "Ian Collins"<> wrote in message
    >> news:...
    >>> 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
     
    Ian Collins, Sep 16, 2010
    #10
  11. * Ian Collins, on 16.09.2010 21:57:
    > On 09/17/10 01:46 AM, Helge Kruse wrote:
    >> "Ian Collins"<> wrote in message
    >> news:...
    >>> 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.


    Oh, I've said it tens of times, the solution for both is a good long whipping
    with course salt applied to the wounds, then tar and feathers for the
    irresponsible people, and a public display of them where we can spit on them.

    Unfortunately, nobody's taken my suggestions seriously enough to implement them.

    I wonder why, it's so obvious.


    Cheers,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
     
    Alf P. Steinbach /Usenet, Sep 16, 2010
    #11
  12. * Ian Collins, on 16.09.2010 21:59:
    > On 09/17/10 07:57 AM, Ian Collins wrote:
    >> On 09/17/10 01:46 AM, Helge Kruse wrote:
    >>> "Ian Collins"<> wrote in message
    >>> news:...
    >>>> 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.


    TB 3.x removes spaces when replying to a "format: flowed" message.

    It's moronic.

    But then so is much software today; I think it reflects that people aren't in
    general promoted for their technical skills, so that in many cases people who
    can imagine that such functionality is good, are those who set the rules.


    Cheers,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
     
    Alf P. Steinbach /Usenet, Sep 16, 2010
    #12
  13. Alf P. Steinbach /Usenet <>, on
    16/09/2010 22:10:46, wrote:

    > * Ian Collins, on 16.09.2010 21:59:
    >> On 09/17/10 07:57 AM, Ian Collins wrote:
    >>> On 09/17/10 01:46 AM, Helge Kruse wrote:
    >>>> "Ian Collins"<> wrote in message
    >>>> news:...
    >>>>> 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.

    >
    > TB 3.x removes spaces when replying to a "format: flowed" message.
    >
    > It's moronic.
    >
    > But then so is much software today; I think it reflects that people
    > aren't in general promoted for their technical skills, so that in many
    > cases people who can imagine that such functionality is good, are those
    > who set the rules.


    Well, at least for this very case, it appears to be a known bug... some
    weeks ago I've struggled a bit to find a mention of it in the TB
    BugZilla and right now I have no handy reference... in any case, you
    know, crap happens, that's not necessarily sign of incompetence or
    deliberate sabotage ;-)

    --
    FSC - http://userscripts.org/scripts/show/59948
    http://fscode.altervista.org - http://sardinias.com
     
    Francesco S. Carta, Sep 16, 2010
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Newsgroup - Ann
    Replies:
    5
    Views:
    642
    John Carson
    Jul 30, 2003
  2. glen stark
    Replies:
    2
    Views:
    725
    Ron Natalie
    Oct 10, 2003
  3. Fraser Ross
    Replies:
    4
    Views:
    1,078
    Fraser Ross
    Aug 14, 2004
  4. Replies:
    25
    Views:
    1,714
  5. AzamSharp
    Replies:
    2
    Views:
    192
Loading...

Share This Page