to store char* from function return value
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);
}
}
You should allocate a buffer at heap with malloc
, and return it. The users of your function will be responsible for deallocating the memory (and your documentation has to clearly state this!)
A simple change would be
char* buffer = (char*)malloc(256);
// beware that now `sizeof(buffer)` will be not 256 any more, but 4, so
// you have to define your constant for it.
...
if (nbytes) return buffer;
free(buffer);
return 0;
...
int main(int argc, char *argv[])
{
char *letter = ReadToSerialPort();
if (letter)
{
printf("Response is %s\n", letter);
free(letter);
return 0;
}
return 1;
}
Please note that the code *bufptr = '\0';
should be before return
, not after!
EDIT
Does your code look like this:
char *ReadToSerialPort()
{
const int buffer_size = 256;
char *buffer = (char *)malloc(buffer_size);
char *bufptr = buffer;
int nbytes;
while ((nbytes = read(fd, bufptr, buffer+buffer_size-bufptr-1)) > 0)
{
bufptr += nbytes;
}
*bufptr = '\0';
if (bufptr != buffer)
return bufptr;
// else cleaning up
free(buffer);
return 0;
}
I am curious where does fd
come from?
Fix up your main code to look like this:
//In main
int main( int argc , char *argv[])
{
char *letter = ReadToSerialPort();
if(letter != NULL)
{
printf("Response is %s\n",letter);
}
}
Make sure you use the buffer declared as static
within ReadToSerialPort()
.....
i.e.:
static char buffer[256];
You need to specify your function a bit more. You can not simply say it "returns a char *
". Where are the characters it points to? In a static buffer? On the heap (allocated by new
)? It looks like you are trying to return a pointer to a local buffer (allocated on the stack), which is an error. Alternatively, return a std::string
.
You should change the signature of ReadToSerialPort() to also inform the caller how many bytes you are returning. So you could do either this:
int ReadToSerialPort(char** data);
or this:
void ReadToSerialPort(char** data, int* num_of_bytes);
and you stay responsible for allocating memory space inside ReadToSerialPort().
The user would do something like (not tested):
int main( int argc , char *argv[])
{
char* data = NULL;
int count = 0;
ReadToSerialPort(data, &count);
if (data != NULL && count > 0) // Let's suppose count returns as 5
{
printf("data[0]:%x data[1]:%x data[2]:%x data[3]:%x data[4]:%x\n", data[0], data[1], data[2], data[3], data[4]);
}
// and the user is responsible for deallocating data himself
free(data);
return 0;
}
精彩评论