Thread: Make a returnable function?

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    16

    Make a returnable function?

    Hi guys,
    i've a function imeino_str. I'm passing a string in imeino pointer variable and printing the value data. Here i want to make that function imeino_str a returnable function. If i give
    printf("%s",imeino_str("\r\n865789024598757 OK\r\n"));
    it should return the string "865789024598757". Is it possible?

    here is my code:


    Code:
    #include<stdio.h>
    
    	int main()
    	{
    		imeino_str("\r\n865789024598757 OK\r\n"));
    		//printf("%s",imeino_str("\r\n865789024598757 OK\r\n"));
    		return 0;
    	}
    	void imeino_str(char *imeino)
    	{
    		 int i;
    	    ///char imeino[100]="\r\n865789024598757 OK\r\n";
    	    char data[20];
    	    for(i=2;i<17;i++)
    	        {
    	            //data[i-2]=imeino[i];
    	    	data[i-2]=imeino[i];
    	        }
    	    data[i-2] = '\0';
    	    printf("%s",data);
    	
    
    
    
    	}
    Thanks...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    When you say "returnable function", I imagine that you want to return the function, which in C means returning a pointer to the function. However, from your description, it appears that this is not so. Rather, you want to return a value from a function, and this value is a string.

    Now, there is no string type in standard C. Rather, a string is a contiguous sequence of characters terminated by a null character. This sequence is stored in an array of some sort, and may be accessed via a pointer to the first character of the string (typically a pointer to the first character of the array that stores the string).

    You cannot return an array from a function, but you can return a pointer. However, the pointer must point to an object that exists even after the function returns, i.e., you cannot create a non-static local array within the function, return a pointer to the first character of that array, then expect it to work.

    There are a few ways around this, including:
    • Have the caller pass a pointer to the first character of an array that already exists, e.g., it was declared in the caller.
    • Use dynamic memory allocation.
    • Use a static local array.

    Using the second option here would allow the syntax that you prefer, but result in a memory leak. Using the third option would also allow the syntax that you prefer, but you may run into mistakes related to returning static local variables (e.g., you call the function more than once then find out that the string that you received from the previous call has changed). Hence, I suggest that you use the first approach, though it requires some modification to what you desire, e.g.,
    Code:
    char *imeino_str(char *destination, const char *source, size_t n)
    {
        /* ... */
        return destination;
    }
    
    int main(void)
    {
        char result[20];
        printf("%s\n", imeino_str(result, "\r\n865789024598757 OK\r\n", sizeof(result)));
        return 0;
    }
    I have added a parameter n for the size of the destination buffer so that imeino_str can be written to avoid buffer overflow. It might not be so important given that imeino_str appears to be trimming the source string, but it is arguably still good practice.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    16
    Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 01-15-2014, 04:15 AM
  2. Replies: 3
    Last Post: 12-10-2013, 07:20 PM
  3. make function?
    By D3V1LD0G in forum C Programming
    Replies: 8
    Last Post: 08-21-2008, 11:23 AM
  4. How to make a function and call it?
    By AmbliKai in forum C Programming
    Replies: 1
    Last Post: 11-14-2007, 10:30 AM
  5. how do you make a function return a value?
    By panfilero in forum C Programming
    Replies: 4
    Last Post: 09-24-2005, 12:48 AM

Tags for this Thread