C++, how to copy char pointer to char array using new producing different addresses?
-
We have an exercise in our training that requires us to use the new operator to copy a char pointer to a char array. I have successfully done that by using a function. From main, a quoted string is passed to a function that accepts a char pointer. And I converted that char pointer to a string, and then converted it to a char array using new operator. I should catch that pointer in the main and then pass it again to the function. When I print the addresses of the char pointers, it is always the same. Should it be different because of the new operator? How can I make it different. Thank you. The functions goes like this: char* fCopyChar(char *oldchar) { string str( oldChar_cp ); char *newChar_cp = NULL; newChar_cp = new char[ str.size( ) + 1 ]; //copy( str.begin(), str.end(), newChar_cp ); int counter_i; for( counter_i =0; counter_i < str.size() ; counter_i++ ) { newChar_cp[ counter_i ] = *oldChar_cp; *oldChar_cp++; } newChar_cp[ str.size( ) ] = '\0'; cout << &newChar_cp << endl; cout << newChar_cp << endl; return newChar_cp; } int main( ) { char *copiedPointer_cp = NULL; copiedPointer_cp = fCopyChar( "This is a string." ); fCopyChar( copiedPointer_cp ); delete[] copiedPointer_cp; return 0; } Thank you again!
-
Answer:
The address of the pointers in the two calls relative to each other is indeterminent. It could be the same, or it could be different. It's likely to be the same because you are calling the same function(fCopyChar) twice from one function(main). So it's likely that the variables are located in the same spot on the stack frame both times. That's not the address of the array that's generated by 'new' though. The pointer holds an address, but it also has an address, which is the thing you are printing here: cout << &newChar_cp << endl; I'm pretty sure that's not what you are trying to do. You are trying to get the address of the array created by 'new', right? To get the address of the array generated by 'new', normally you would just print the pointer, unadorned by any symbols like '&', like you're doing here: cout << newChar_cp << endl; However, char pointers get special treatment when passed to printing functions, which print them as null terminated strings instead of addresses. To get the actual address then, you would want to cast it to a void pointer, like this: cout << (void*)newChar_cp << endl;
blowfish at Yahoo! Answers Visit the source
Other answers
I see two problems with your code. The parameter in the declaration of your copy function is listed as oldchar, but in the body of the function you reference oldChar_cp. This might be a typo when you typed in the code here, since I don't see how this would compile correctly, and you state that you do get output, so I don't think this is a problem. The other one, though, is in your two cout statements. I think the first one should be oldChar_cp instead of printing out newChar_cp both times. By the way, in the main procedure you call fCopyChar twice, so two arrays are created, but you only delete the first one. This is called a memory leak, since the second array never gets deleted. In this simple program this isn't a problem, since the memory is recovered as soon as the program exits, but you should learn to avoid this type of thing now, because if it occurs in a more complicated program it will cause problems, problems that can be very hard to track down in the debugging phase.
Carl
Related Q & A:
- How to convert a char array into an int in C++?Best solution by Stack Overflow
- How to convert from string to char in C++?Best solution by Stack Overflow
- How to programatically select a item in list using c#?Best solution by Software Quality Assurance & Testing
- Converting char array to double in C?Best solution by stackoverflow.com
- How to copy each row from a worksheet into new workbooks?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.