Converting string to char*

  • Follow


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:













7/10/2012 8:38:00 PM


Reply: