Solved

retunr type

Posted on 2007-04-08
3
193 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
Comment Utility
#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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 create, access, and change arrays in the C programming language.

743 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

8 Experts available now in Live!

Get 1:1 Help Now