Solved

Function that returns a charpointer

Posted on 2011-02-17
9
385 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
undefined reference to `bswap_128' 9 160
Problem with MFCApp 78 380
Using unmanaged DLL from managed application 14 294
Constant string is of type char *   ? 7 28
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

786 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