1

I've just started using C programming language for coding my course project and I'm not much knowledgeable about C. I've been using C++ for a while now and I need to find an alternative to c_str() function in C. I'm trying to code (in C) similar to a c++ code below. I'm absolutely clueless how to do the same. Any help is greatly appreciated.

void putVar(double* var,int N, string name, Engine *ep){
    double row = N, col = N;
    mxArray *matlab = mxCreateDoubleMatrix(row, col, mxREAL);
    double *pa = mxGetPr(matlab);
    memcpy(pa, var, sizeof(double)*row*col);
    engPutVariable(ep, name.c_str() , matlab);
}
Pranav
  • 560
  • 1
  • 8
  • 21
  • You simply pass `name` as a `char*`, rather than as a `std::string`. c_str() simply returns a `char*` pointer to the data contained within the string class. – enhzflep Jul 16 '14 at 19:18
  • 2
    `c_str()` gives you a `char*` pointer to a C-style `'\0'`-terminated string given a C++ `std::string` object. C doesn't have `std::string`, so there is no equivalent. `name` needs to be a `char*` rather than a `string`, and you need to manage storage for it yourself rather than letting `std::string` manage it for you. – Keith Thompson Jul 16 '14 at 19:18
  • 1
    notes on above: substitute `char*` with `char const*` – WhozCraig Jul 16 '14 at 19:20
  • C is not object oriented. You pass char arrays around directly, or build some kind of struct to abstract it a little. Everything you can do in c is also possible in c++ (almost always with the exact same code), so you might as well stick to c++. – Dave Jul 16 '14 at 19:20
  • string ain't a C basic data type unless you defined that type by yourself.So u can't just use that in C – mSatyam Jul 16 '14 at 19:22
  • Thanks @enhzflep for the reply, its working now! And Sorry for absolutely noob question. – Pranav Jul 16 '14 at 19:29
  • 1
    @Pranav - you're welcome. Dont worry, Chess-masters start out by losing. I.e we get better with time - both at the coding part and at the knowing what/how to research to find the info want. – enhzflep Jul 16 '14 at 19:33
  • @Pranav Make `name` a `char const *` instead of `char *`. You don't need to modify it anywhere within `putVar()` – Praetorian Jul 16 '14 at 19:46

1 Answers1

7

I need to find an alternative to c_str() function in C...

As stated in the comments above, C does not have a string type, but C does use arrays of char, and when NULL terminated are commonly referred to as C strings.
There are many various methods to create strings in C. Here are three very common ways:

Given the following: (for illustration in this example)

#define MAX_AVAIL_LEN sizeof("this is a C string") //sets MAX_AVAIL_LEN == 19

1

char str[]="this is a C string";//will create the variable str, 
                                //populate it with the string literal, 
                                //and append with NULL. 
                                //in this case str has space for 19 char,
                                //'this is a C string' plus a NULL 

2

char str[MAX_AVAIL_LEN]={0};//same as above, will hold 
                      //only MAX_AVAIL_LEN - 1 chars for string
                      //leaving the last space for the NULL (19 total).
                      //first position is initialized with NULL

3

char *str=0;  
str = malloc(MAX_AVAIL_LEN +1);//Creates variable str,
                             //allocates memory sufficient for max available
                             //length for intended use +1 additional
                             //byte to contain NULL (20 total this time)

Note, in this 3rd example, Although it does not hurt,
the "+1" is not really necessary if the maximum length of
string to be used is <= the length of "this is a C string".
This is because when sizeof() was used in creating MAX_AVAIL_LEN,
it included the NULL character in its evaluation of the string
literal length. (i.e. 19)
Nonetheless, it is common to write it this way when allocating memory for C strings
to show explicitly that space for the NULL character has been considered during memory allocation.

Note 2, also for 3rd example, must use free(str); when finished using str.

Look here for more on C strings.

ryyker
  • 22,849
  • 3
  • 43
  • 87