Hi,
This was the routine I wrote earlier to convert a C++ string to a
char array.
But I found that the char* array consisted only of junk after returning
from the below function.
int convertStringToChar(string& str, char* data)
{
data = (char*)malloc(str.length() * sizeof(char));
memset((void*)data,0,sizeof(data));
strcpy(data,str.c_str());
return 0;
}
However, I replaced it with the following function and it worked just
fine.
int convertStringToChar(string& str, char** data)
{
*data = (char*)malloc(str.length() * sizeof(char));
memset((void*)*data,0,sizeof(data));
strcpy(*data,str.c_str());
return 0;
}
1. Can you tell me the reason?
2. Is the second routine defect free? Note that I'm not terminating the
char* array with a "\0" anywhere.
FYI, the compiler being used is gcc 2.96.
Thanks and Regards,
Karthik.
|
|
0
|
|
|
|
Reply
|
karthik.naig (8)
|
4/15/2005 9:52:59 AM |
|
karthik.naig@gmail.com wrote:
> Hi,
>
> This was the routine I wrote earlier to convert a C++ string to a
> char array.
>
> But I found that the char* array consisted only of junk after
returning
> from the below function.
>
> int convertStringToChar(string& str, char* data)
> {
> data = (char*)malloc(str.length() * sizeof(char));
> memset((void*)data,0,sizeof(data));
> strcpy(data,str.c_str());
> return 0;
> }
>
> However, I replaced it with the following function and it worked just
> fine.
>
> int convertStringToChar(string& str, char** data)
> {
> *data = (char*)malloc(str.length() * sizeof(char));
> memset((void*)*data,0,sizeof(data));
> strcpy(*data,str.c_str());
> return 0;
> }
>
> 1. Can you tell me the reason?
> 2. Is the second routine defect free? Note that I'm not terminating
the
> char* array with a "\0" anywhere.
>
> FYI, the compiler being used is gcc 2.96.
>
> Thanks and Regards,
> Karthik.
why dont you use str.c_str()?
|
|
0
|
|
|
|
Reply
|
ranveerkunal (24)
|
4/15/2005 1:52:01 PM
|
|
karthik.naig@gmail.com wrote:
>
> Hi,
>
> This was the routine I wrote earlier to convert a C++ string to a
> char array.
What's a C++ string?
> But I found that the char* array consisted only of
> junk after returning from the below function.
>
> int convertStringToChar(string& str, char* data)
You can't have a definition like
string& str
in C.
If str is a structure, then the C way is
convertStringToChar(string *str, char* data)
and the function call looks like this
convertStringToChar(&str, data)
and the members would be referenced like this
str -> c_str()
The big problem, is that your string is copied to data,
and data is unknown outside your function.
Your function should return the return value of malloc
as a char*.
What else is strange is that you don't use the initial value
of the data parameter, you just write over it.
The memset call is pointless.
> {
> data = (char*)malloc(str.length() * sizeof(char));
> memset((void*)data,0,sizeof(data));
> strcpy(data,str.c_str());
> return 0;
> }
>
> However, I replaced it with the following function and it worked just
> fine.
>
> int convertStringToChar(string& str, char** data)
> {
> *data = (char*)malloc(str.length() * sizeof(char));
> memset((void*)*data,0,sizeof(data));
> strcpy(*data,str.c_str());
> return 0;
> }
>
> 1. Can you tell me the reason?
> 2. Is the second routine defect free?
> Note that I'm not terminating the
> char* array with a "\0" anywhere.
You didn't leave room for a "\0" anywhere.
You should add one to the string length, to allocate for a string.
I don't know what type string is, since this a C newsgroup,
so I made one up a type called str_ing and defined it.
This is how it would be done in C code.
/* BEGIN new.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
size_t length;
char *(*c_str)(void);
} str_ing;
char *cppstring(void);
char *convertStringToChar(str_ing *ptr);
int main(void)
{
char *array;
str_ing str;
str.length = strlen(cppstring());
str.c_str = cppstring;
array = convertStringToChar(&str);
if (array != NULL) {
puts(array);
free(array);
}
return 0;
}
char *convertStringToChar(str_ing *ptr)
{
char *data = malloc(1 + ptr -> length);
if (data != NULL) {
strcpy(data, ptr -> c_str());
}
return data;
}
char *cppstring(void)
{
return "string";
}
/* END new.c */
--
pete
|
|
0
|
|
|
|
Reply
|
pfiland (6613)
|
4/15/2005 2:50:50 PM
|
|
|
2 Replies
32 Views
(page loaded in 0.071 seconds)
Similiar Articles: convert string to ascii - comp.soft-sys.matlabHello Can anybody tells how to convert a string in Ascii values ... Hello Can anybody tells how to convert a string in ... or ASCII, encoding ... How To: Convert Character ... char to string - comp.soft-sys.matlabI have a file name, which is a character array, and i want to place this name into a matrix. To do this, i've tried to convert the char array into a string. varchar2 to hex/octal ... convert string - comp.databases.oracle ...is there way to 'dump' a string (varchar2) to the screen in hex? "this is a string" would print something like "33 44 23 64 345 43 34 ..." (I ju... converting a character string into a variable name - comp.lang ...I have a FORTRAN question. The problems is: I want to write several variables, arrays and scalars, out to a file. I want to store the names of ... Converting UTF-16 character strings to ISO8859-1 - comp.os.vms ...Are there any native OpenVMS character string conversion functions that will convert from UTF-16 to ISO8859-1? Thanks in advance for any wisdom, ... help with convert real value to character - comp.lang.fortran ...converting a character string into a variable name - comp.lang ... help with convert real value to character - comp.lang.fortran ..... insert a single literal quote ... converting .NET System.String objects to matlab strings - comp ...Can anyione tell me how to convert a .NET System.String object to a MATLAB ... C# Convert Char Array to String - Dot Net ... How to convert from System::String* to ... Convert all cell array to string - comp.soft-sys.matlabConvert to character array (string) - MATLAB To convert characters into a numeric array, use the double function. S = char(C), when C is a cell array of strings, places ... Converting number to std::string ("itoa()" ) - comp.lang.c++ ...... such a conversion function eg. by looping (eg via %) through the digits, d, in the number, and convert each to a character via eg 'a'+ d, and appand this to a string. 'scramble' string - comp.lang.javascriptHi, I am trying to do a very simple "encryption" of a text string in java script. For instance, if the user enters : steve, I want to just convert each character to ... How to convert from System::String* to Char* in Visual C++Describes several ways to convert from System::String* to char* by using managed extensions in Visual C++ How to: Convert Between Various String TypesThis topic demonstrates how to convert various Visual C++ string types into other strings. The strings types that are covered include char *, wchar_t*, _bstr_t ... 7/10/2012 8:38:00 PM
|