Let's see an example below.
main()
{
char* pPtr;
MyStringCopy(pPtr, "hello");
}
MyStringCopy(char* dest, char* src)
{
dest = new char[sizeof(src)];
strcpy(dest, src);
}
The upper program returns wrong answer becuase the main function's pPtr has not assigned no memory space and indicates wrong position.
You should know that MyStringCopy's dest value is just copy value from main's pPtr.
The upper code is mapped with the attached left picture.
To resolve that problem, you can approach like below.
main()
{
char* pPtr;
MyStringCopy(&pPtr, "hello");
}
MyStringCopy(char** dest, char* src)
{
*dest = new char[sizeof(src)];
strcpy(dest, src);
}
In the upper case, you deliver the pPtr's address value. And MyStringCopy's dest value is copy of the pPtr pointer's address value( not pointer value). That means both funcions indicate same pPtr value. ( in other words, both functions know pPtr's value position. )
The upper code is mapped with the attached right picture.
'잡담' 카테고리의 다른 글
after listening to a big data lecture (0) | 2014.05.21 |
---|---|
According to Aristotle, nature makes nothing in vein, for all that God creates is done with a purpose. (0) | 2014.05.13 |
플라톤과 노자 (0) | 2014.05.01 |
lessons learned from wingtip notification project (0) | 2014.04.30 |
소프트웨어 교육이란? (0) | 2014.04.28 |