Pointer as a function parameter

E

errfet

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 :)
 
I

Ian Collins

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)
 
S

Stuart Golodetz

errfet said:
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
 
R

red floyd

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;
}
 
R

red floyd

First of all, note that<cstring> in<string.h> in the std::namespace.
Oops. said:
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;
}
 
H

Helge Kruse

Ian Collins said:
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
 
D

Default User

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
 
I

Ian Collins

Ian Collins said:
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

Ian Collins

Ian Collins said:
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.
 
A

Alf P. Steinbach /Usenet

* Ian Collins, on 16.09.2010 21:57:
Ian Collins said:
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
 
A

Alf P. Steinbach /Usenet

* Ian Collins, on 16.09.2010 21:59:
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
 
F

Francesco S. Carta

on said:
* Ian Collins, on 16.09.2010 21:59:
On 09/17/10 01:46 AM, Helge Kruse wrote:
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 ;-)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top