![]() |
|
#1
|
|||
|
|||
Problem Passing Pointer in return..(Possibly)Well recently I've decided to get a head start on then C programming course being taught next year at the school I attend. I've been reading though some tutorials, and in a learning attempt I tried to re-create the MID function from BASIC language. Everything seems to work inside the function, but when the pointer is passed back to main() it gets garbeled. Any help is greatly appreciated.
CPP / C++ / C Code:
|
|
#2
|
||||
|
||||
|
Hello and Welcome to GIDForums Pr0fess0r. What are you compiling with and what are you actually getting. The only thing that pops out is you may be having problems due to passing back a reference to a local variable (rtn).
Unless I'm missing something it seems to work with gcc 3.4.1 under CygWin. Code:
hth Mark __________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work." --Thomas Alva Edison "Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety." --Benjamin Franklin "A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes." --Hugh Downs |
|
#3
|
|||
|
|||
|
Hmm, odd. When I run it I get:
This is a test. Thi ╦┤Ç| Compiled Using Dev-C++ / gcc 3.4.2 |
|
#4
|
|||
|
|||
|
Yeah, thanks for the tip. With this revision, seems to work just fine.
char *nw; nw = (char *) malloc((end-start)+2*sizeof(char)); strcpy(nw,rtn); return nw; So for further reference, what the heck happened? When the function ended did it pass the pointer back to main, then destroy the information in it? I don't understand, both the new and old methods, were just pointers, only the first was created using an array, and the second was dynamically allocated via malloc(). |
|
#5
|
||||
|
||||
|
Being a local variable rtn ceased to exist when you went back to main and you tried to print it. You just got the junk from the memory location. I got the correct output but by no means would I always. I believe that is in the "undefined" area of the language. At least I think that is what happened.
Glad to help. Mark __________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work." --Thomas Alva Edison "Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety." --Benjamin Franklin "A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes." --Hugh Downs |
|
#6
|
|||
|
|||
|
another way to (other than malloc()) is to define array rtn as static type. This way it is persistent even when the function mid() returns to main(). Static variables are unlike auto (default type of local variables for any functions) variables. They are persistent.
Just make this change in your mid function CPP / C++ / C Code:
|
Recent GIDBlog
Stupid Management Policies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Re: Things to Avoid in C/C++ -- gets() , Part 1 | WaltP | C Programming Language | 5 | 21-Jun-2007 13:13 |
| Problem with one variable | bretter | C++ Forum | 1 | 16-May-2005 08:20 |
| [Tutorial] Pointers in C (Part II) | Stack Overflow | C Programming Language | 0 | 27-Apr-2005 18:36 |
| C++ file I/O | CronoX | C++ Forum | 36 | 09-Mar-2004 18:28 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The