help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Passing a string to an external function


From: John W. Eaton
Subject: Passing a string to an external function
Date: Fri, 5 May 2000 03:51:45 -0500 (CDT)

On  5-May-2000, Ryan Shepperd <address@hidden> wrote:

| Dear Community
| 
| I'm not a C++ programmer but I managed to write most of a wrapper function
| for an external C program, which I need to call from Octave.  The C
| function expects arguments of type "float" and "char."  I've successfully
| passed the C-function its float arguments by first using a type cast in
| the following manner:
|   ColumnVector x = args(0).vector_value ();
|   some_float_var = (float) x(0);
| This works great.  Unfortunately, I have not been able to figure out,
| after a couple days, how to convert an Octave "string" to a "char."  I
| can access the string from inside the wrapper function by
|   string filename = args(1).string_value ();
| but I can't find a way to convert the string to a char array (char[30]) in
| order to pass the argument to the C function.  If this is really quite
| trivial, I apologize for wasting bandwidth; however, I have done my best
| grep through the help archive and example wrapper functions. :) 
| 
| If someone more knowledgeable could tell me how to type cast from
| "string" to a "char," I would be grateful.

If what you want is really char*, then use

  string filename = args(1).string_value ();
  char *p = filename.c_str ();

If you really need char[30], then something like

  const int N = 30;
  string filename = args(1).string_value ();
  char buf[N];
  strncpy (buf, filename.c_str (), N-1);
  buf[N-1] = '\0';

might be more appropriate (assuming that the array needs to be
nul-terminated).

jwe



-----------------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.che.wisc.edu/octave/octave.html
How to fund new projects:  http://www.che.wisc.edu/octave/funding.html
Subscription information:  http://www.che.wisc.edu/octave/archive.html
-----------------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]