c - get pointer of functions for use on other fuctions -
c - get pointer of functions for use on other fuctions -
i need help
i want homecoming string in function , utilize on do this?
char *randstring() { int n = (0 + rand() % ( 4 - 0 )); char cadena[5][100] ={ {"duck"}, {"taxi"}, {"hola mundo!"}, {"paris"}, {"lexugon"} }; homecoming cadena[n] ; }
and want on main function , send function user...
int main (){ srand(time(null)); int contador = 0; /*while ( contador < 100 ){ char* ptr = randstring(); printf ( "%s\n", ptr ); contador++; }*/ char *ptr=randstring(); compare( ptr ); getchar(); homecoming 0; }
you cannot homecoming string comes local array in automatic storage, if declared array static
and/or moved declaration outer scope:
static char cadena[5][100] ={ {"duck"}, {"taxi"}, {"hola mundo!"}, {"paris"}, {"lexugon"} }; char *randstring() { homecoming cadena[rand() % 5] ; }
you not allowed reference variables in automatic storage area when go out of scope; undefined behavior. objects in static storage remain valid.
declaring cadena
static in outer scope ensures name remain "invisible" other translation units, ensuring declaration of cadena
in different c file not create name collision.
c function pointers
Comments
Post a Comment