GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Jun-2005, 18:52
Pr0fess0r Pr0fess0r is offline
New Member
 
Join Date: Jun 2005
Posts: 5
Pr0fess0r is on a distinguished road

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:
#include <stdio.h>

char * mid(char *x, int start, int end);
     
main(){
       char *msg = "This is a test.";
       puts(msg);    
       printf("\n%s",mid(msg,1,3));   
       getchar();
}

char * mid(char *x, int start, int end){  //MID accepts the string, the first character, and the last character to be in a new string
    int ctr = 0;
    char rtn[(end-start)+2];
    
    for(ctr = start;ctr<=end;ctr++){
            rtn[ctr-start] = x[ctr-1]; 
    }
    
    rtn[(end-start)+1] = '\0';
    printf("%s",rtn);
     
    return rtn;
}

  #2  
Old 28-Jun-2005, 19:20
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
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:
OUTPUT: $ BasicTest This is a test. Thi Thi

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  
Old 28-Jun-2005, 19:32
Pr0fess0r Pr0fess0r is offline
New Member
 
Join Date: Jun 2005
Posts: 5
Pr0fess0r is on a distinguished road
Hmm, odd. When I run it I get:
This is a test.
Thi
╦┤Ç|

Compiled Using Dev-C++ / gcc 3.4.2
  #4  
Old 28-Jun-2005, 19:46
Pr0fess0r Pr0fess0r is offline
New Member
 
Join Date: Jun 2005
Posts: 5
Pr0fess0r is on a distinguished road
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  
Old 28-Jun-2005, 20:46
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
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  
Old 29-Jun-2005, 00:54
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
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:
char * mid(char *x, int start, int end){  //MID accepts the string, the first character, and the last character to be in a new string
    int ctr = 0;
    static char rtn[(end-start)+2];  /* make rtn a static */
    
    for(ctr = start;ctr<=end;ctr++){
            rtn[ctr-start] = x[ctr-1]; 
    }
    
    rtn[(end-start)+1] = '\0';
    printf("%s",rtn);
     
    return rtn;
}
 
 

Recent GIDBlogStupid Management Policies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 08:21.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.