|
-
February 2nd, 2005, 11:14 PM
#1
Passing address of a char* and then initializing and setting it
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|