0

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 for the usage of the strcpy and many others links relevant to this topic but I didn’t get it..

1
  • Instead of ignoring your warnings and errors, it is helpful to read them and analyze them to understand what you're doing wrong so you can do it right next time. Commented Nov 23, 2013 at 15:06

5 Answers 5

3

Declare your function as

char *myPrint(int n);

Do not return the local variable msg from your function - it ceases to exist after myPrint function.

 if(n==1)
     return "ACCEPT";
 else
     return  "DENY";
Sign up to request clarification or add additional context in comments.

2 Comments

ok but i get this now 10:8: error: incompatible types when assigning to type ‘char[10]’ from type ‘char *’
That is because char * is not the same as char[10]. If you declare myPrint as having return value of char[10] this error should disappear.
1

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;
} 

Comments

1

You should never return a pointer to variable that is allocated in the function, because by the time it returns, the variable will be de-allocated automatically (it is stack-allocated as opposed to heap-allocated or static). So instead, you pass in a char * to myPrint and fill the char array from inside it. Then the outer function that declared the char array can access it without error. Note though that the following is still not best practice, because it allows for buffer overruns when one passes in a pointer to an array that is shorter than length of "ACCEPT" + 1 (+1 for terminating \0 character).

void myPrint(char *msg, int n)
{
    if (n == 1)
    {
        strcpy(msg, "ACCEPT");
    }
    else
    {
        strcpy(msg, "DENY");
    }
}

int main()
{
    char msg[10];
    myPrint(msg, 1);
    printf("\n %s \n", msg); 
}

Comments

0

Declare your function as

char *myPrint(char *ptr, int n);  

to return string from your function.
Call it from your main function as

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

Your function

char *myPrint(char *msg, int n)
{ 
    if(n==1)
         strcpy(msg, "ACCEPT");
    else
         strcpy(msg, "DENY");

    return msg;
}    

But in this case there is no need to return string from your function as it will update your msg array in main.

3 Comments

See the update. You cannot return pointer to local variable. You have to pass an address to your function.
Now please explain why downvote? He wanted to return pointer from his function.
@Nat95; Thanks :). I suggest you to use debugger to debug your program. This will help you to catch these type of errors.
-1
#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;
}

7 Comments

-1: Why are you allocating msg in 'main' first, then overwrite it with the newly allocated buffer from 'myPrint'? And then you don't even free the buffers. And you don't check for unsuccesful 'malloc'.
Because if I don't allocate it in 'myPrint' program gives segmentation fault.
The first allocation in main is completely useless and gives you unreachable memory when you overwrite the pointer to it with the return value from 'myPrint'!
Ok no problem let's initialize it in main with NULL, but you have to allocate it in 'myPrint'. Otherwise program gives segmentation fault, because it doesn't know how many bytes allocate to the string!
Seriously? free(myPrint(1)) ? Please don't post this kind of non-sense. You clearly have no understanding of how this stuff works.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.