using char* as reference variable and read from a function

Hi,
I want to read a string (char *). i.e, I want to send char* argument
and after calling the function I want the argument to be filled in that function definition.
i.e, allocation of memory and assiging value is done in that function definition.
And if i print the value of that in main function i shd get it.

Ex:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
file4.cpp
void fun2(string &s)
{
   s = "defined";
}

file3.cpp
void fun1(char* receivedString)
{
   string str;
   fun2(str);
   strcpy(receivedString, str.c_str());
}

file2.c
void fun(char* receivedString)
{
   fun1(receivedString)
}

file1.c
int main()
{
   char *strToBeAssignedInFun;
   fun(strToBeAssignedInFun);
   printf("string is : %s", strToBeAssignedInFun);
}

You haven't actually specified your problem, but I'm guessing you get a memory error.

You need to allocate memory for your char* strToBeAssignedToInFun before you copy to it. Otherwise, to where will the data be copied?

Also, you should probably consider using C++ strings throughout. Is there any particular reason for this giant chain of functions, or is it just an experiment? ;)
I have also tried allocating memory using malloc()

file4.cpp
void fun2(string &s)
{
s = "defined";
}

file3.cpp
void fun1(char* receivedString)
{
string str;
fun2(str);
char* temp = str.c_str();
receivedString = (char *) malloc(sizeof(char) * str.size()+1);
for(int i=0; i<str.size(); i++,receivedString++ temp++)
*receivedString = *temp;
*receivedString = '\0';
}

file2.c
void fun(char* receivedString)
{
fun1(receivedString)
}

file1.c
int main()
{
char *strToBeAssignedInFun;
fun(strToBeAssignedInFun);
printf("string is : %s", strToBeAssignedInFun);
}

But still segmentation fault
Use str.length(), not str.size(). str.size() includes the null terminator character.
Last edited on
Nah, it's equivalent to length(). C++ strings aren't null-terminated.
error is on Line 12 - please read the man pages on strcpy()

the requirement is that the first argument must have its space already allocated, but if you trace back the variable receivedString, it goes all the way back to Line 24 which is a char* which hasn't been initialized!!!

if you use your code in your OP and change Line 24 to:
1
2
char carray[80];  // needs to be big enough to take in Line 4's "defined" or anything you put there
char *strToBeAssignedInFun = carray;  // initialize character pointer 

it should run with that change - I haven't tried your code with this fix yet so I'm assuming there are no other bugs in your code

btw, you can mix C character arrays, char* and C++ strings as much as you like, but make sure you know what you are doing

also, you may consider using strdup() instead of strcpy() if you are worried about assigning a fixed array to strToBeAssignedInFun, but if you do use strdup(), you must remember to free()


Last edited on
Thanks every one...
Its solved ..
Topic archived. No new replies allowed.