我想從函數返回一個字符串(在示例funzione)到main。這個怎麼做?謝謝!從函數返回一個字符串到主
#include <stdio.h>
#include <string.h>
#define SIZE (10)
/* TODO*/ funzione (void)
{
char stringFUNC[SIZE];
strcpy (stringFUNC, "Example");
return /* TODO*/;
}
int main()
{
char stringMAIN[SIZE];
/* TODO*/
return 0;
}
將帖子對於那些誰需要它,以前的代碼的完整版本(但不stringMAIN)是:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE (10)
char *funzione (void)
{
char *stringa = malloc(SIZE);
strcpy (stringa, "Example");
return stringa;
}
int main()
{
char *ptr = funzione();
printf ("%s\n", ptr);
free (ptr);
return 0;
}
你甚至嘗試過搜索嗎? –
如果你不理解它,你應該繼續搜索你不瞭解的每件作品,並學會將它應用於你的問題。 Stackoverflow並不是要爲你做你的工作。 –