Question: I am learning C and I want to implement a function that returns to main function a string. I have read some topics here but I got confused.. I wrote:

```
#include <stdio.h>
#include <string.h>

char myPrint(int); 

int main()
{ 
   char msg[10];

   msg = myPrint(1);

   printf("\n %s \n", msg); 
} 

char myPrint(int n)
{ 
   char msg[10];

   if(n==1)
      strcpy(msg, "ACCEPT");
   else
      strcpy(msg, "DENY");

   return msg;
}

```

and I get a bunch of warnings and error.. I read [this](http://www.tutorialspoint.com/c_standard_library/c_function_strcpy.htm) for the usage of the strcpy 
and many others links relevant to this topic but I didn’t get it..

Answer: 
A bit of advice: returning a pointer to a string when that string is defined as a local variable in the function won't work, as the allocated memory for the string will be unvalidated.

If your function returns literal strings, as per your example, you have to do as **Acme** said:

```
char *MyPrint (int n)
{
  if (n==1)
    return "ACCEPT";
  else
    return "DENY";
}

```

As you are returning a pointer to char, you cannot assign that pointer to an array, as you try to do in `main()`. You have to assign it to a variable that must be a pointer to char as well:

```
int main()
{ 
   char *msg;

   msg = myPrint(1);    
   printf("\n %s \n", msg); 
   return 0;
} 

```

If you need to copy the resulting string to an array, then you must use `strcpy()` (or better yer, `strncpy()` ) as this:

```
int main()
{ 
   char msg[10];

   strcpy (msg, myPrint(1) ); /* providing that your function won't ever return a string longer than 9 chars, or bad things will happen */
   printf("\n %s \n", msg); 
   return 0;
} 

```
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define STR_SIZE 10

char * myPrint(const int ); 

int main()
{ 
    char *msg = NULL;

    msg = myPrint(1);

    free(myPrint(1));

    printf("\n %s \n", msg);

    return 0;
} 

char * myPrint(const int n)
{ 
    char *msg = (char *) malloc(STR_SIZE * sizeof(char));

    if (n == 1)
        strcpy(msg, "ACCEPT");
    else
        strcpy(msg, "DENY");

    return msg;
}

```