Solved

retunr type

Posted on 2007-04-08
3
203 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
[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
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

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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers 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.

691 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