![]() |
| ||
| String to char* conversion Hello, I've had some problems converting a string into a char. I've tried many, many things, but it didn't seem to work. I'll post the code below:
As you can see, I'm making a string that will contain the URL, I place the URL in it and then want to convert it to a char*. I really tried about everything. The URL doesn't neccesarely have to be a string, but I thought that would be the easiest way. The errors that I get are: Compiling... line 169 is url = sUrl.c_str(); and line 173 is hFile = InternetOpenUrl( hINet, (char*)sUrl, ULL, 0, 0, 0 );Greetz, Eddy Edit: The error is in the strcpy() line, weird thing is that if I remove that line (put // before it) it still crashes. I will try to find out why. |
| ||
| Re: String to char* conversion url = sUrl.c_str(); This won't work because you can't assign one Cstyle string to another. At the top of your code add the line #include <cstring> and then use this: strcpy(url, sUrl.c_str()); for the above line. There are other rules for using C style strings. If you are going to be using them routinely you should probably find a good C/C++ book and learn about them They are also called null terminated char arrays. |
| ||
| Re: String to char* conversion char* url; --------------------Configuration: TibiaName - Win32 Debug-------------------- and craaaaaash... The program crashes without returning any data. I didn't find the error yet by breakpoints. I will try to continue the search.. Thanks anyway Greetz, Eddy |
| ||
| Re: String to char* conversion Remember where I said it would be a good idea to read about C style strings if you were going to use them? This is probably an example why. url is declared as a char *, however it probably never had any memory given to it to point to. Therefore, when you try to write to it using the strcpy() call, boom. You can either change url to a fixed size char array using static memory, and make it big enough to hold any likely string you will end up putting into it, or you can use dynamic memory to make url just the right size. //using dynamic memory char * url; url = new char[sUlr.length() + 1]; strcpy(url, sUlr.c_str()); //remember free up memory for url before using it again or when done with it by calling: delete[] url; |
| ||
| Re: String to char* conversion You shouldn't have to bother with all that, since InternetOpenUrl expects LPCTSTR (typedef as const TCHAR*) as its second argument. Try: hFile = InternetOpenUrl( hINet, sUrl.c_str(), NULL, 0, 0, 0 ); |
| ||
| Re: String to char* conversion > char *url; > url = sUrl.c_str(); Which got you error C2440: '=' : cannot convert from 'const char *' to 'char *' Understand that the c_str() method returns a const pointer for a reason, and that's to try and stop you from sneaking in the back door to change the string. > char *url; > strcpy(url, sUrl.c_str()); Which got you warning C4700: local variable 'url' used without having been initialized Yes, you used a variable before you initialised it. Expect string data to be sprayed all over some unknown memory. You can either allocate it as Lerner suggests, Or simply have const char *url;to satisfy the const requirements. Or just do what GloriousEremite showed, and scrap the whole temporary used once thing. |
| ||
| Quote:
------------------------------------------------ Hi instead of url=sUrl.c_str() u can write like url=(char *)(sUrl.c_str()) leme know if u have any comments.. |
| ||
| The easy way would be to have url as a char*. Or use different library. Microsoft library for strings has got the method to convert from string to char*, so if you would use it instead, you could convert your type by calling this method. |
| All times are GMT -4. The time now is 4:32 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC