473,322 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Converting from a char* to a string

So I have looked everywhere and cannot find a way to do this. I am using VC++ Express 2005 to develop my code.

I am using the function strtok_s() to break apart a char*, and the return type of strtok_s is a char*.

I have the following code:

>>>string final ;
>>>char tmp1[100] ;
>>>char* tmp2 ;
>>>char* next ;
>>>
>>>tmp2 = strtok_s(tmp1, " ", &next) ;
>>>cout << "tmp2 = " << tmp2 << endl ;

I then want to assign whatever is in tmp2 (the output from the cout function from what I see is what I want) to final, doing this:

final = tmp2 ;

However, when I pass final to one of my functions that takes a string the function breaks. An error box pops up saying:

>>>Debug Assertion Failed!
>>>
>>>Program: ...
>>>File: memcpy_s.c
>>>Line: 55

>>>Espression: dst != NULL

>>>For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.

What is going on? And how can I make it so that final (when passed to my function that is basically defined as foo(string x) {} ) will work properly?

Any help is greatly appreciated, thanks a ton!
Nov 30 '07 #1
3 2478
weaknessforcats
9,208 Expert Mod 8TB
You need to allocate memory for tmp2.

strtok_s copies the token there.
Nov 30 '07 #2
How do I do that?

Would I use malloc() because that seems a little bit out dated, just in my opinion. If you could show me some psuedo-code or actual code that would be greatly appreciated, or even jsut a reference to what function I should use.

Thanks!
Nov 30 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
If you are writing in C, malloc is not out-dated. You use it to allocate memory at run time.

Maybe here you could allocate memory at compile time by usin a char array for tmp2 instead.

Remember, in C and C++ the name fo an array is the address of element 0 so tmp2 can still be used as a char*.
Dec 1 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do...
4
by: Prabhu | last post by:
Hi, We are having problem in converting a byte array to string, The byte array has char(174), char(175), char(240), char(242) and char(247) as delimiters for the message. when we use...
18
by: Marcio Kleemann | last post by:
I need to force the first letter of each word in a line of text to uppercase. The text comes from a TextBox control in a Web Form. I'm new to ..NET and am having a problem. Since I can't modify...
3
by: fakeprogress | last post by:
How would I go about converting this C code to C++? /* LIBRARY is an array of structures */ /* This function compares 'tcode' with */ /* existing codes in the array. */ /* It...
2
by: pookiebearbottom | last post by:
Just looking for opinion on which of the 3 methods below people use in their code when they convert a 'const char *' to a 'const std::string &' came across #3 in someone's code and I had to...
0
by: anide | last post by:
Hi all I’ve some problem, I’m trying to converting a sorting algorithm from C++ to C#. In C++ I’ve compiled it using MSVC and its working properly, and in C# I’m using .NET Framework 2.0 (Visual...
2
by: CoreyWhite | last post by:
Problem: You have numbers in string format, but you need to convert them to a numeric type, such as an int or float. Solution: You can do this with the standard library functions. The...
5
by: Hans Mull | last post by:
Hi! How can I convert a string to a const unsigned char*? (string::c_str() converts the string to a signed char) Thanks in advance, Hans
1
by: vcbytes | last post by:
I am having a problem with the following code: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { String^ texts = textBox1->Text; char *text =...
4
by: screamer81 | last post by:
Hello, I've a SDK functions description for a scanner and I try to convert unmanaged DLL C++ functions to c#. I converted all except one of them. This function is getting a struct parameter. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.