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
Solved

retunr type

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

Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
memory leak detection 9 94
Using unmanaged DLL from managed application 14 294
Coverting 24 hour time to 12 hour in C++ 15 178
List out all word 7 317
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
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.

856 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