Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Function that returns a charpointer

Posted on 2011-02-17
9
Medium Priority
?
391 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1400 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1400 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 1400 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

704 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