Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,117 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,765 people online right now. Registration is fast and FREE... Join Now!




Returning a pointer array

 
Reply to this topicStart new topic

Returning a pointer array

Pontus
6 Jun, 2007 - 09:51 AM
Post #1

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 529



Thanked: 2 times
Dream Kudos: 275
My Contributions
Here is my code to return a pointer-array-thingie

CODE
char* convert(string from){
char*  to=new char[100];
for(int a= 0;a<from.size();a++)*(to+a)=from[a];
*(to+from.size())=0;
return to;
}

Now how cane i make this work with this?:
CODE

string b = "hello";
char phrase[] = convert(b);

Any help is appreciated
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Returning A Pointer Array
6 Jun, 2007 - 10:30 AM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
char *phrase = convert(b);
User is offlineProfile CardPM
+Quote Post

Pontus
RE: Returning A Pointer Array
6 Jun, 2007 - 11:32 AM
Post #3

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 529



Thanked: 2 times
Dream Kudos: 275
My Contributions
But it must be used like this:
CODE
char phrase[]=convert("hello");

not
CODE
char* phrase=convert("hello");

User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Returning A Pointer Array
6 Jun, 2007 - 06:53 PM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
#1 Why "must it be used" like that?

You see the two things are relativity different to the compiler. Yes a char[] and a char * are very similar, but a char[] is a non-dynamic allocated array. Its size must be known by the compiler so that it knows how much room to set aside on the stack. The notation:
char Array[] = "This is a test";
is an initialization line. Where the compiler determines how much space is needed and sets it aside.

Your program creates a dynamic array (though why you limited yourself to 100 chars I don't know) and then copies the contents of a string into this array. Unless you want a memory leak, you will have to return that pointer, so that later you can use delete to destroy the array when it is no longer needed.

There might be some way to overload either the assignment or [] operator to let this work... but I doubt it.

There are other approaches.

You realize that this is cyclic though right?

By calling convert("hello"); you are forcing the compiler to change a string literal (const char *) into a string, which you then convert back to a c-string... Why not just say char hello[] = "world";? skip the middle man.

...so why must it work like this?
User is offlineProfile CardPM
+Quote Post

Pontus
RE: Returning A Pointer Array
7 Jun, 2007 - 07:32 AM
Post #5

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 529



Thanked: 2 times
Dream Kudos: 275
My Contributions
ok, ill do it that way, thx
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:55PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month