Solved

retunr type

Posted on 2007-04-08
3
197 Views
Last Modified: 2010-04-15
can someone give me a simple example of a function whose return type is string or character array?
0
Comment
Question by:perlperl
3 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 18874059
#include <string.h>

     char *
     stpcpy(char *dst, const char *src);

     char *
     strcat(char *s, const char * append);

     char *
     strncat(char *s, const char *append, size_t count);

     char *
     strchr(const char *s, int c);

     char *
     strrchr(const char *s, int c);
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18874721
And with the body :

    char *returnString() {
        char *str = calloc(10, sizeof(char)); /* creating a 9-character string on the heap */
        /* filling up the string here ... */
        return str;
    }

Call it like this :

    char *myString = returnString(); /* call the function which returns the string */
    /* use the string */
    free(myString); /* don't forget to clean up the string once you don't need it any more, or you have a memory leak */
0
 
LVL 3

Expert Comment

by:dongjinkim
ID: 18883453
I think the usual convention is create a buffer first (either using the heap or stack), then pass a pointer to the buffer as an argument to the function so the function can modify it directly. This allows the function to handle strings, regardless of the method of creation.

Getting to your question, if you want the function to return a string, the normal way is to create it on the heap (dynamic memory allocation), since the stack will be destroyed once the function exits and the pointer will reference memory that is no longer valid. In this scenario, the user is responsible for freeing up the memory used by the string once he/she is done.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
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.

910 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

16 Experts available now in Live!

Get 1:1 Help Now