vikrampschauhan
February 2nd, 2005, 10:14 PM
Say we have a function that calls Func2:
void Func1()
{
char* str1;
Func2(&str1);
std::cout<<str1;
}
Func2 does the following:
void Func2(char** str2)
{
*str2=new char[10];
strcpy(*str2,"Hello")
}
When execution goes into Func1, following things happen:
1. It is seen that the address of str1 is taken. So, str1 is allocated on the stack. The address of str1 is given to Func2.
2. Func2 is called and address of str1 is given to str2. str1 is given a valid address(by saying *str2=new char[10])
3. str1 is given the string "Hello" - by saying strcpy(*str2,"Hello")
4. When Func2 ends, execution goes back to Func1. The line std::cout<<str1 prints "Hello"
I believe I am correct?
Thanks
Vikram
void Func1()
{
char* str1;
Func2(&str1);
std::cout<<str1;
}
Func2 does the following:
void Func2(char** str2)
{
*str2=new char[10];
strcpy(*str2,"Hello")
}
When execution goes into Func1, following things happen:
1. It is seen that the address of str1 is taken. So, str1 is allocated on the stack. The address of str1 is given to Func2.
2. Func2 is called and address of str1 is given to str2. str1 is given a valid address(by saying *str2=new char[10])
3. str1 is given the string "Hello" - by saying strcpy(*str2,"Hello")
4. When Func2 ends, execution goes back to Func1. The line std::cout<<str1 prints "Hello"
I believe I am correct?
Thanks
Vikram