Search code examples
carraysstring2dreturn-type

How to Return The Address of String(As Char Pointer)


My file include just 1 word (max 20 character) in every line and it has 100 line also.I am trying to read random line from file,and put it in char *str this random string.I need the address of the word to use the string read later(because after i will print it in 2d array (like word puzzle). How should I return their address or what is needed to print it in different function late?Could you show me one example about that,ı am searching it about 1 day read random word from file and still not enlightened.

char read(char *file, char *str);
return (char *)str;

is not working as I expected there is warning which is

returning ‘char *’ from a function with return type ‘char’ makes integer from pointer without a cast.


Solution

  • First of all, do not use the same name as library functions for user-defined functions, read() is a well-known POSIX library function.

    That said, the error (or warning) message is quite clear, there's a type mismatch. To return a char *, you need to have a function with return type of char *. Define the function having char * as return type, like

    char * my_read (.....
    

    instead of

    char my_read (.....