Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

char* pointer from string in C#

Tags:

c#

char

pointers

Is it possible to get a char* for a string variable in C#?

I need to convert a path string to a char* for using some native win32 function ...

like image 349
Kris Avatar asked Nov 01 '09 20:11

Kris


People also ask

Is char * A string in C?

This last part of the definition is important: all C-strings are char arrays, but not all char arrays are c-strings. C-strings of this form are called “string literals“: const char * str = "This is a string literal.

Can a char pointer point to a string?

Instead of using arrays, we can use character pointers to store a string value.

Is char * a string?

char* is a pointer to a character. char is a character. A string is not a character. A string is a sequence of characters.


1 Answers

Well you could certainly do this:

string str = "my string";  unsafe  {     fixed (char* p = str)     {                        // do some work     } } 

where there is an operator (char*) bound to the string object. However, the output format may not be compatible with the underlying C or other format... this is however quite a good solution to parse the string. Hope it's helpful for anybody who reads this post.

like image 198
Guillau pelletier Avatar answered Sep 20 '22 19:09

Guillau pelletier