Ask Your Question
1

Problem with subscriber a string [closed]

asked 2013-02-08 08:03:30 -0500

mrpiccolo gravatar image

updated 2014-01-28 17:15:10 -0500

ngrennan gravatar image

Hi, I tried to do the tutorial writing a simple publisher and subscriber in c++ and it works.
Now i must use a function with argument the string that was subscribed, but i received the error: argument incorrect. This is the code:

void chatterCallback (const std_msgs::String::ConstPtr& msg) {
ROS_INFO("I heard: [%s]\n", msg->data.c_str());
my_function(msg->data); //error
// i also tried msg->data.c_str()
} void my_function(char* c) ....

any suggestion?
thanks

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by ayush_dewan
close date 2013-02-15 08:29:00

Comments

What is the signature of my_function?

Jeffrey Kane Johnson gravatar imageJeffrey Kane Johnson ( 2013-02-09 02:23:24 -0500 )edit

Also relevant: Does my_function need a non-const char* or is the just an old-style definition. If it doesn't change it, you could get away with just passing in the string data.

dornhege gravatar imagedornhege ( 2013-02-09 02:30:22 -0500 )edit

3 Answers

Sort by ยป oldest newest most voted
0

answered 2013-02-08 23:20:02 -0500

msg->data is of type string and ur function input argument is of type char*. Therefore u cannot directly pass either msg->data or msg->data.c_str() as argument and since u cannot change ur function argument i suggest following. Add this to ur subscriber callback

char *c; c=new char[sizeof(msg->data)]; c=&what[0]; myfunction(c)

This should work.

PS.Let me know if this doesnt work or any of the assumptions i made regarding ur problem is wrong.

edit flag offensive delete link more

Comments

This looks like a memory leak to me. You are reassigning the pointer to c and forgetting the newly allocated memory. In addition sizeof does NOT return the length of the string.

dornhege gravatar imagedornhege ( 2013-02-09 01:23:40 -0500 )edit
1

I think you're looking for the strcpy function; just re-assigning the pointer won't change anything.

Jeffrey Kane Johnson gravatar imageJeffrey Kane Johnson ( 2013-02-09 02:26:21 -0500 )edit
1

thanks i tried strcpy(c,msg->data.c_str()); myfunction(c); and now work, because strcpy convert const char* to char*.

mrpiccolo gravatar imagemrpiccolo ( 2013-02-09 07:40:59 -0500 )edit

@mrpiccolo please accept this answer using the checkmark on the left side so others know your question is resolved.

tfoote gravatar imagetfoote ( 2013-02-11 10:44:25 -0500 )edit
0

answered 2013-02-08 22:21:08 -0500

mrpiccolo gravatar image

thanks for the answer
but i can't do it because my_function is a method of a library that was installed in my pc, so i can't change the argument.

edit flag offensive delete link more
0

answered 2013-02-08 13:55:19 -0500

You could do something like this:

void chatterCallback(const std_msgs::String::ConstPtr& msg)
 {
  my_function(msg);
 }

 void my_function(const std_msgs::String::ConstPtr& c)
 {
  ROS_INFO("I heard from my function: [%s]", c->data.c_str());
 }
edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-02-08 08:03:30 -0500

Seen: 884 times

Last updated: Feb 08 '13