Solved

Function that returns a charpointer

Posted on 2011-02-17
9
383 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now