to store char* from function return value
Posted
by
samprat
on Stack Overflow
See other posts from Stack Overflow
or by samprat
Published on 2010-12-22T15:44:58Z
Indexed on
2010/12/22
15:54 UTC
Read the original article
Hit count: 133
Hi folks, I am trying to implement a function which reads from Serial Port ( Linux) and retuns char*. The function works fine but how would I store return value from function. example of function is
char *ReadToSerialPort()
{
char *bufptr;
char buffer[256]; // Input buffer/ /
//char *bufptr; // Current char in buffer //
int nbytes; // Number of bytes read //
bufptr = buffer;
while ((nbytes = read(fd, bufptr, buffer+sizeof(buffer)-bufptr -1 )) > 0)
{
bufptr += nbytes;
// if (bufptr[-1] == '\n' || bufptr[-1] == '\r')
/*if ( bufptr[sizeof(buffer) -1] == '*' && bufptr[0] == '$' )
{
break;
}*/
} // while ends
if ( nbytes ) return bufptr;
else return 0;
*bufptr = '\0';
} // end ReadAdrPort
//In main
int main( int argc , char *argv[])
{
char *letter;
if(strcpy(letter, ReadToSerialPort()) >0 )
{
printf("Response is %s\n",letter);
}
}
© Stack Overflow or respective owner