1

How to return a local character array from one function

char* testfunction()
{
char array[] = "Hello World";
 return array;
}

char main()
{
 char* array = testfunction();
 printf(" %s -> string", array);
 return 0;
}

THis code results unknown error

�@$�@<��Ʉ؅�;���Y@��� -> string

billz
  • 44,644
  • 9
  • 83
  • 100
Sijith
  • 3,740
  • 17
  • 61
  • 101

4 Answers4

7

You are returning a pointer which points to a local variable, when testfunction() returns array in main() becomes dangling pointer.

use std::string instead

#include <string>
#include <iostream>

std::string testfunction()
{
    std::string str("Hello World");
    return str;
}

int main()
{
    std::cout << testfunction() << std::endl;
    return 0;
}
billz
  • 44,644
  • 9
  • 83
  • 100
5

You should not return the address of the stack variable directly as it is destroyed once the stack frame is removed(after the function returns).

You could do this.

#include <stdio.h>
#include <algorithm>

char* testfunction()
{
   char *array = new char[32];
   std::fill(array, array + 32, 0); 
   snprintf(array, 32, "Hello World");
   return array;
}

int main()
{
   char* array = testfunction();
   printf(" %s -> string", array);
   delete[] array;
   return 0;
}
HAL
  • 3,888
  • 3
  • 19
  • 28
  • If you now say what he should do isntead it would even be an answer ;) – dhein Aug 26 '13 at 12:43
  • Chance tehre to distroy the memory, – Sijith Aug 26 '13 at 12:48
  • 1
    Unless you enjoy debugging memory leaks, return `std::string` (or some other [RAII](http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization) object) rather than hoping the caller knows what to do with a dumb pointer. – Mike Seymour Aug 26 '13 at 13:08
  • @MikeSeymour You're right, but since the question was about returning an array from a function, I gave this solution. – HAL Aug 26 '13 at 13:11
1

Perhaps, this is what you want:

const char *testfunction(void)
{
        return "Hello World";
}
mesmerizingr
  • 1,417
  • 1
  • 18
  • 25
0

You can't return the address of a local variable. (if you use gcc you should get some warnings)

You can try to use the keyword static instead:

char    *test()
{
  static char array[] = "hello world";
  return (array);
}
Alexis
  • 2,149
  • 2
  • 25
  • 39
  • This will cause interesting bugs when someone modifies it, and future calls return the modified value. (And if it's not meant to be modified, there's no point in the array at all - just return a `const` pointer to the literal). – Mike Seymour Aug 26 '13 at 13:13
  • well we don't know what the op want to do with it. Hoover in the op code we can see only c and not c++. But can you explain why you can get bugs ? – Alexis Aug 26 '13 at 13:17
  • As I said: if anyone modifies the array returned by this function, then future calls will return the modified value. The code in the question tries (but fails) to return the same value each time; your code behaves in a surprisingly different way to that. (I suppose you could modify your version to rewrite the value each time; then you've just got reentrancy and thread-safety to worry about). – Mike Seymour Aug 26 '13 at 13:20
  • I guess my comprehension of English is not good enough and I haven't understood that the op tries to return the same value each time (How to return a local character array from one function) Furthermore he declare a `char*` and not a `const char*` Anyway it's a duplicate. (For someone who doesn't look at the warnings/errors of the compiler) – Alexis Aug 26 '13 at 13:31