String To Char* :(

Started by
5 comments, last by dabihsss 7 years, 8 months ago

So i convert lets say its a std::string to char *

anyway debugger claims its there, but whenever i try to get value from char* it says its null and i get access violation 000000

i think its because of * in a void parameter anyway heres a code:

typedef std::string AnsiString;


void main()
{
int p;
char * u = 0;
AnsiString pk = "R";
CopyToPCHAR(pk, &p, u);



ShowMessage(AnsiString(u,p)); //error

}


template <class type> void DeleteIfPersist(int * len, type * p)
{
 if (p != 0) delete [] p;
 p = 0;
 (*len) = 0;
}

inline void CopyToPCHAR(AnsiString str, int * len, char * p /* i think heres a problem */)
{
DeleteIfPersist(len, p);

(*len) = str.Length();

if ((*len) > 0)
{
p = new char[ (*len) ];
strcpy(p, str.c_str());
}

}


so what is going on?
Advertisement
CopyToPCHAR needs to take either a pointer or reference to char*, otherwise all changes are done to local variables and lost on return.

p = new char[ (*len) ];


That's not enough space to store the string and a terminating zero.



if ((*len) > 0)


What's the point of that condition? What would you like to happen if str is an empty string?

Thanks all changing to:

inline void CopyToPCHAR(AnsiString str, int * len, char *& p) solved the case

Alvaro my program by default uses char* without null terminator

[spoiler]

i have another question because i have similar problem:


template <class type> void Copycptr(const int * s, type * sp, int * d, type *& dp) //changed it like above from type * dp
{
DeleteIfPersist(d, dp);
(*d) = (*s);
int k = (*d);

if (k <= 0) return;
dp = new type[ k ];
for (int i=0; i < k; i++) dp[i] = sp[i]; //and i have error here with the same access violation 00000 thing
}

i try to copy with that function both int * or char *

for ints

Copycptr(&in.qual_len, in.qualID, &qual_len, qualID);

where qual_len is int

and int * qualID;

for chars

Copycptr(&in.stanowisko_len, in.stanowisko, &stanowisko_len, stanowisko);

int stanowisko_len

char * stanowisko;

boths in. structure is

employee& operator=(const employee& in)

and crap it may be something else, andi changed whole application base code, to use that and didint check it till now x_X

[/spoiler]

Spoiler is fixed because i was trying to copy pointer1 to pointer1 instead of pointer1 to pointer2 :/

Copycptr(&in.ulen[month], uwagi[month], &ulen[month], uwagi[month]); where i had to write:

Copycptr(&in.ulen[month], in.uwagi[month], &ulen[month], uwagi[month]);

And alvaro i just wrote that if statement because if theres an empty string to copy = do nothing. (all pointers and lengths are zeroed)

Alvaro my program by default uses char* without null terminator


Sure, but strcpy doesn't care what conventions you have in your head.

strcpy(p, str.c_str());

That's not enough space to store the string and a terminating zero.

Just got that error, luckily you mentioned that.

a bucket of kittens for you :P

bucket-of-kittens-featured.jpg

I was also facing the same issue. Thank you for the help

Finest web hosting solution at the most affordable rates web hosting india cheap vps hosting india

This topic is closed to new replies.

Advertisement