C Programming

Returning String from a C Function

A string is a sequence of characters which consists of either a single character, or multiple characters and is an important data structure in computer programming. When working with strings in C, certain operations are required to be performed on the string data type such as initializing the string, assigning content, concatenating the strings, comparing strings, and returning the string.

This article focuses on one such operation in the string data structure, which is returning a string from a C function.

Returning String from a C Function

As string is an array of characters and to return string from a C function, a pointer to the string data structure needs to be passed as an argument from the calling function to the called function as a parameter. The pointer can then be used to point to an array of characters. Once the pointer points to an array of characters, the string value can be returned from the called function using the return statement.

You can look at the below-given example for better understanding.

#include <stdio.h>

const char* myName() {
  return "John";
}

int main(void) {
  printf("%s", myName());
}

 

In the above code, instead of declaring the variable like we do normally, we used a pointer with it so that when we return this string, no error occurs.

Output


A C function cannot return a string that has been specified as a local variable since the variable will be immediately removed (released) when the function has finished running, rendering it unavailable and triggering a warning as below:

#include <stdio.h>

const char myName() {
  char name = "John";
  return name;
}

int main(void) {
  printf("%s", myName());
}

 

In this code, a warning is triggered as string is being returned without any pointer.

Output


So, you can write the code in the following way:

#include <stdio.h>

const char* myName() {
  char *name = "John";
  return name;
}
int main(void) {
  printf("%s", myName());
}

 

Output


Variables are allocated on the stack by default, which is the actual reason for the above statement. However, when a pointer is declared, the value it points to gets allocated on the heap, which is not cleared after the function is finished. Once the memory is allocated, a copy of the string needs to be created from the original memory location pointed by the pointer and the copy needs to be returned from the called function. The calling function will then have control over the original string data, while the called function is responsible for freeing the allocated space.

Conclusion

A C function can take a pointer to string as argument and return the string value as a result of the operations carried out within the called function. The approach to achieve this is using a pointer of char*. The calling function will be responsible for the memory allocated to store the string, while the called function is responsible for freeing the allocated space.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.