Next, you are trying to return a local value by reference. buff is local to myfunction(). You need to pass by value, not by reference.
Next, your indexing is off. C based indexing starts at 0 and goes to size -1.
buff[25] hasn't been allocated. Valid addresses are buff[0] to buff[24].
If you use buff[25] = "Hai" as assignment then only buff[0], buff[1], buff[2] have been defined. Anything else is just what is memory.
Here is an example using a global variable.
Code:
#include <stdio.h>
#include <string.h>
char myfunction(void);
char buff[3];
int main ( void /*int argc, char *argv[]*/ ){
printf("Before myfunction()...\n");
puts(buff);
printf("After myfunction()...\n");
myfunction();
puts(buff);
return 0;
}
char myfunction(void)
{
int i = 1;
strcpy(buff, "Hai");
//buff = "Hai";
i = i + 1;
return buff[0];
}
//char str_value = myfunction(void);
Here is using pass by reference:
Code:
#include <stdio.h>
#include <string.h>
char * myfunction(char *);
char buff[3];
int main ( void /*int argc, char *argv[]*/ ){
char buff[3];
printf("myfunction()...\n");
puts(myfunction(buff));
return 0;
}
char * myfunction( char * buff_pass )
{
int i = 1;
strcpy(buff_pass, "Hai");
//buff = "Hai";
i = i + 1;
return buff_pass;
}
//char str_value = myfunction(void);