Converting string to char*

K

karthik.naig

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.
 
D

Darius

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()?
 
P

pete

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 */
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top