Pass const char* to function in C


Dan Brenner

I'm trying to write a function that takes a const char *as a parameter, but I don't know how to pass such data to the function in a useful way. If I have the function:

void tokenize(const char * c) { }

I want to call this function with a hardcoded value (eg backslash), what is the best way? How can I convert the hardcoded value to const char *?

斯特 斯特 · vest Blagovest Buyukliev)

String literals are perfectly legal and can be used in place of const char *parameters:

tokenize("\\");

A more illustrative example would be to store a pointer to the string, and then pass that pointer:

const char *token = "\\";
tokenize(token);

However, char *even if the compiler allows it, string literals are not compatible . This has to do with how string literals are stored - they are usually in a read-only area of ​​memory, and trying to write to them will result in undefined behavior, most likely some kind of protection fault.

Related


Pass const char* to function in C

Dan Brenner I'm trying to write a function that takes a const char *as a parameter, but I don't know how to pass such data to the function in a useful way. If I have the function: void tokenize(const char * c) { } I want to call this function with a hardcoded

Pass const char* to function in C

Dan Brenner I'm trying to write a function that takes a const char *as a parameter, but I don't know how to pass such data to the function in a useful way. If I have the function: void tokenize(const char * c) { } I want to call this function with a hardcoded

How to pass const char* to C function from C#?

scientific I try to call a pure C function from an external DLL in a C# application. This function is defined as void set_param(const char *data) Now I have some problems using this function: How can I specify this "const" in C# code? public static extern voi

How to pass const char* to C function from C#?

scientific I try to call a pure C function from an external DLL in a C# application. This function is defined as void set_param(const char *data) Now I have some problems using this function: How can I specify this "const" in C# code? public static extern voi

How to pass const char* to C function from C#?

scientific I try to call a pure C function from an external DLL in a C# application. This function is defined as void set_param(const char *data) Now I have some problems using this function: How can I specify this "const" in C# code? public static extern voi

How to pass const char* to C function from C#?

scientific I try to call a pure C function from an external DLL in a C# application. This function is defined as void set_param(const char *data) Now I have some problems using this function: How can I specify this "const" in C# code? public static extern voi

How to pass const char* to C function from C#?

scientific I try to call a pure C function from an external DLL in a C# application. This function is defined as void set_param(const char *data) Now I have some problems using this function: How can I specify this "const" in C# code? public static extern voi

How to pass const char* to C function from C#?

scientific I try to call a pure C function from an external DLL in a C# application. This function is defined as void set_param(const char *data) Now I have some problems using this function: How can I specify this "const" in C# code? public static extern voi

How to pass const char* from python to C function

马赫什(Mahesh Chaurasia) I'm writing in C++ using open files ctypesin Python . my C++ code: extern "C" { void openfile(const char *filename) { cout<<"File to open for writing = " <<filename<<endl; FILE *fp = fopen(filename,"w"); fprintf(fp,"writing in

Pass char pointer to C function

tiktak I am trying to get the mac address with the following code: void getMacAdress(unsigned char **address) { int s; struct ifreq buffer; s = socket(PF_INET, SOCK_DGRAM, 0); memset(&buffer, 0x00, sizeof(buffer)); strcpy(buffer.ifr_name,

Pass buffer (char*) to function in C

Roger Tannous I am passing a buffer (char*) to a function in C. Inside the function, I'm allocating memory for the buffer and appending a string (response from the virtual server). When printed inside the function, the string will appear as the string sent fro

Pass char pointer to C function

tiktak I am trying to get the mac address with the following code: void getMacAdress(unsigned char **address) { int s; struct ifreq buffer; s = socket(PF_INET, SOCK_DGRAM, 0); memset(&buffer, 0x00, sizeof(buffer)); strcpy(buffer.ifr_name,

Pass buffer (char*) to function in C

Roger Tannous I am passing a buffer (char*) to a function in C. Inside the function, I'm allocating memory for the buffer and appending a string (response from the virtual server). When printed inside the function, the string will appear as the string sent fro

Pass buffer (char*) to function in C

Roger Tannous I am passing a buffer (char*) to a function in C. Inside the function, I'm allocating memory for the buffer and appending a string (response from the virtual server). When printed inside the function, the string will appear as the string sent fro

Pass char pointer to C function

tiktak I am trying to get the mac address with the following code: void getMacAdress(unsigned char **address) { int s; struct ifreq buffer; s = socket(PF_INET, SOCK_DGRAM, 0); memset(&buffer, 0x00, sizeof(buffer)); strcpy(buffer.ifr_name,

Pass const char instead of std::string as function parameter

Abruzzo Forte and Gentile This is a newbie question, but I don't understand how it works. Suppose I have the following function void foo(const std::string& v) { cout << v << endl; } and the call in my program below. foo("hi!"); Essentially, I'm passing a

C function prototype: \char *strinv(const char *s);

green bow char *strinv(const char *s); //that's the given prototype I'm not very safe with the *strinv part. Does this mean that the function is automatically dereferenced when called? Or is the function defined as a pointer? Thanks in advance for the clarifi

C function prototype: \char *strinv(const char *s);

green bow char *strinv(const char *s); //that's the given prototype I'm not very safe with the *strinv part. Does this mean that the function is automatically dereferenced when called? Or is the function defined as a pointer? Thanks in advance for the clarifi

C return const char pointer or char pointer from function

username I want to understand this better, so I'm asking here. I wrote a function that reads a file and returns the content as a string. It's currently implemented to return a char*because it seems easier, but I'm wondering if this is the correct way to do it,

C function prototype: \char *strinv(const char *s);

green bow char *strinv(const char *s); //that's the given prototype I'm not very safe with the *strinv part. Does this mean that the function is automatically dereferenced when called? Or is the function defined as a pointer? Thanks in advance for the clarifi