Solved

Function that returns a charpointer

Posted on 2011-02-17
9
387 Views
Last Modified: 2012-05-11
Hey all,
Need a refresher here...been ages since I wrote a function that returns a charpointer.

Is the following safe?

#define CHECK_MODE 100
#define PROCESS_MODE 200

// return a string that contains the mode currently in use
char *ShowMode(int mode)
{
   char mode_buff[20];

     case CHECK_MODE
        strcpy(mode_buff, "CHECK MODE");
        break;

     case PROCESS_MODE:
        strcpy(mode_buff, "PROCESS MODE");
        break;

     default:
        mode_buff[0] = 0;
        break;

    return (mode_buff);
}

Usage:
void test()
{

    int mode;

    <set the mode here>
     printf("The mode is %s\n", ShowMode(mode));
}

My concern is that I have some vague memory that for functions that return a charpointer, the variable you return must be a function argument (or perhaps a static char declared locally within the function).  My way, you have a local char, but it's not static.

Please advise.
Thanks,
Steve

0
Comment
Question by:Stephen Kairys
  • 5
  • 4
9 Comments
 
LVL 4

Author Comment

by:Stephen Kairys
ID: 34921475
Update...
I just tried the above function, and it apparently returned garbage when I display its returnvalue.
I then changed the local var mode_buff to be static and it apperas to have worked.

So, would appreciate knowing why that change should make a differnce and if I've gone far enough by making the local var static (if at all possi ble I want to avoid adding a paramter to this function.

Thanks again!
Steve
0
 
LVL 40

Accepted Solution

by:
evilrix earned 350 total points
ID: 34921560
No it is not safe since mode_buff is destroyed when the function ends.

Do this instead...
char const *ShowMode(int mode)
{
   switch(mode)
   {
     case CHECK_MODE
        return "CHECK MODE"; // literals live for the life of the program

     case PROCESS_MODE:
        return "PROCESS MODE";
   }
   
   return 0;

}

Open in new window

0
 
LVL 4

Author Comment

by:Stephen Kairys
ID: 34921581
OK, but don't static vars live for the life of the program. It seemed to work when I changed my local var to be a static. Is that OK? Thanks
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 40

Expert Comment

by:evilrix
ID: 34921592
>> but don't static vars live for the life of the program
They do but why bother when you can just return a pointer to a literal?
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 350 total points
ID: 34921602
>> Is that OK? Thanks
All places that reference the pointer reference mode_buff and when this is changed all places referencing it will see that change. That may or may not be what you want. Also, if the code is multi-threaded you will have to handle synchronisation otherwise you'll end up with a race condition.
0
 
LVL 4

Author Comment

by:Stephen Kairys
ID: 34921663
mode_buff will never change unless I need it again via a call to the function.
code is not multi-threaded.

Just curious - I assume that the reason that it's OK to have something like this

int square_of(int i)
{
    int local_i

    local_i = i*i;
    return (local_i);
}

is becuase I am NOT reutrning a pointer to something, right?
Thanks. I think I'm going to playh it safe and return the actual literal :)
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 350 total points
ID: 34921679
>> is becuase I am NOT reutrning a pointer to something, right?
Correct... you are returning a copy of what is in local_i and not a reference to local_i that will go out of scope when the function ends.

>> I'm going to playh it safe and return the actual literal :)
Unless there is a compelling reason not to it is the simplest (and most efficient) solution.
0
 
LVL 4

Author Comment

by:Stephen Kairys
ID: 34921688
OK. Trying out your sol'n now, will post back after I confirm it works in the my application. Thanks.
0
 
LVL 4

Author Closing Comment

by:Stephen Kairys
ID: 34921750
THANK YOU!! That worked. Have a good evening.
-Steve
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question