Return a string from function to main

后端 未结 3 1874
南笙
南笙 2020-12-18 17:28

I want to return a string from a function (in the example funzione) to main. How to do this? Thank you!

#include 
#include 

        
3条回答
  •  情书的邮戳
    2020-12-18 18:03

    You can do this as

    char *funzione (void)
    {
        char *stringFUNC = malloc(SIZE);
        strcpy (stringFUNC, "Example");
    
        return stringFUNC;
    }  
    

    In main, call it as

    int main()
    {
        char stringMAIN[SIZE];
        char *ptr = funzione ()
        ...
    
        free(ptr);
        return 0;
    }
    

提交回复
热议问题