Pointers. Part 4. Pointers and strings. Using pointers when converting strings
This topic is based on the following topics:
Contents
- 1. How to declare an unmanaged pointer (*) to the string literal? Examples
- 2. How to define a pointer to a string that is defined as an array of characters? Example
- 3. How can I get the value of the i-th element of a character string using an unmanaged pointer (*)? Example
- 4. Convert from char to String. An example of using a managed pointer (^)
- 5. Conversion from wchar_t to String. An example of using a managed pointer (^)
- 6. Conversion from string to System::String. An example of using a managed pointer (^)
- 7. Conversion from String to char. An example of using a managed pointer (^)
- 8. Conversion from char to wchar_t. An example of using a unmanaged pointer (*)
- 9. Conversion from String to wchar_t. An example of using a managed pointer (^)
- Related topics
Search other websites:
1. How to declare an unmanaged pointer (*) to the string literal? Examples
Details about the literal is described here.
A string of characters is a string literal, which is an array of char characters. The address of the first character of the string literal is the initial value of the pointer. A pointer to the first element of a string literal is a pointer to the entire string literal.
Example. Pointer ps to string literal. Attempting to change the character in the string literal.
// a pointer to a string literal char * ps2 = "This is a text"; // access to the character of a string using the pointer //*ps2 = 't'; - Error, you can not change the string literal through the pointer
Error occurs when you try to change the character string literal:
Attempted to read or write protected memory
2. How to define a pointer to a string that is defined as an array of characters? Example
The name of the array of characters is a pointer-constant. If a string is described as an array of characters, then you can change the characters of the string using the pointer.
Example 1. A pointer to an array of characters. Access to the character of a string, which is described as an array of characters.
// a pointer to an array of characters char str1[] = "Text"; // array str1 = { 'T', 'e', 'x', 't', '\0' } char *p1; // way 1 - the name of the string is a pointer to the first character of the string p1 = str1; // pointer p1 points to str1 // way 2 p1 = &str1[0]; // access to a string of characters via a pointer *p1 = 'N'; // str1 = "Next" - it's works *(p1+3) = '!'; // str1 = "Nex!"
3. How can I get the value of the i-th element of a character string using an unmanaged pointer (*)? Example
Example. Reading a character in a character string using the pointer.
// Reading a character in a character string using the pointer char * p1 = "Hello world!"; char c; c = *p1; // c = 'H' c = *(p1+1); // c = 'e' c = *(p1+2); // c = 'l' c = *(p1+3); // c = 'l' // reading a character using a pointer char * str = "Hello world!"; char * p2 = str; c = *p2; // c = 'H' c = *(p2+1); // c = 'e' c = *(p2+12); // c = '\0'
4. Convert from char to String. An example of using a managed pointer (^)
To work with character strings, the C/C++ CLI language supports the System::String class. In this class, there are many methods for convenient handling of character strings.
To convert from char to String is used a managed pointer.
Example 1. Conversion from type char* to type String.
// conversion from char* to String char * s = "Text"; String ^str; // pointer to class System::String str = gcnew String(s); // str = "Text"
Example 2. Conversion from type char[] to type String.
#include <cstring> ... // conversion from char[] to String char s2[50]; strcpy(s2,"Text-2"); str = gcnew String(s2); ...
In the above example, we used the function
strcpy(s2,"Text-2");
which copies the string “Text-2” to the string s2.
To use this function, you need to include the <cstring> or <string.h> library.
5. Conversion from wchar_t to String. An example of using a managed pointer (^)
The wchar_t type supports large character sets, for example Unicode. To convert from wchar_t to String, it is advisable to use a managed pointer ^.
For more information about the features of the type wchar_t in С/C++ CLI is described here.
Example. Conversion from type wchar_t[] to type String.
// conversion from wchar_t* to String wchar_t * sw = L"Text Unicode"; String ^str; // pointer to class System::String str = gcnew String(sw); // conversion // conversion from wchar_t[] to String wchar_t s2[] = L"Text Unicode";
6. Conversion from string to System::String. An example of using a managed pointer (^)
The C++ CLI language supports the “string” type for working with character strings. To convert from string to System::String, use the c_str() method.
To use the string type in programs, you need to connect the <string> library and the std namespace at the beginning of the module
#include <string> using namespace std;
Example. Conversion from string to System::String.
#include <string> using namespace std; ... // conversion from string to System::String string s = "Text"; String ^str = gcnew String(s.c_str()); // str = "Text"
7. Conversion from String to char. An example of using a managed pointer (^)
The conversion takes place in 2 stages.
At the first stage, a conversion is made from the String type to the wchar_t type. This is done using the function
PtrToStringChars(s)
where s – string of type String. Function PtrToStringChars() returns an object of type wchar_t.
At the second stage, a conversion from wchar_t to char is performed. This is done using the wcstombs() function, which takes 5 parameters. Below is a description wcstombs() function:
errno_t wcstombs_s(size_t * numChars, char * mbs, size_t size_mbs, const wchar_t *wcs, size_t len);
where
- numChars – the number of characters that have been converted (converted);
- mbs – the address of the memory buffer in which the result will be written;
- size_mbs – the size of the memory buffer for characters that are converted;
- wcs – a string that is converted (the original string);
- len – the maximum length of the source string wcs or the value _TRUNCATE.
The size_t type is the result of the sizeof() operator and is the length of the object.
Example.
// conversion from String to char String ^s = gcnew String("Text"); // source string char res[20]; // result string // 1. Converting from String to wchar_t pin_ptr <const wchar_t> wstr = PtrToStringChars(s); // 2. Converting from wchar_t to char[] size_t d; d = 0; wcstombs_s(&d, res, (size_t)20, wstr, _TRUNCATE);
8. Conversion from char to wchar_t. An example of using a unmanaged pointer (*)
Example. Conversion from char* to wchar_t*.
// conversion from type char* to type wchar_t* char *src = "Text-2"; // source string size_t src_size; // length of source string size_t dest_size = 30; // buffer for the string after conversion size_t res_size = 0; // the length of string after conversion wchar_t dest[30]; // result string src_size = strlen(src) + 1; // get the length of the original string + 1 // function mbstowcs_s() mbstowcs_s(&res_size, dest, src_size, src, _TRUNCATE); // dest = L"Text-2"
In the above example, is used the function
strlen(src)
which is implemented in the module <cstring> or <string.h>. The function returns the length of the string src.
The function mbstowcs_s() gets 5 parameters and has a declaration:
errno_t mbstowcs_s(size_t *numChars, wchar_t *wcs, size_t size_wcs, const char *mbs, size_t len);
where
- numChars – the number of characters that have been converted;
- wcs – the address of the memory buffer in which the result will be written (type wchar_t*);
- size_wcs – the size of the memory buffer for characters that are converted;
- mbs – a string that is converted (the original string);
- len – the maximum length of the original string mbs or the value of _TRUNCATE.
The size_t type is the result of the sizeof() operator and is the length of the object.
9. Conversion from String to wchar_t. An example of using a managed pointer (^)
In this conversion is used method
pin_ptr <const wchar_t> PtrToStringChars(dest)
where the input parameter dest is a string of type String. The method returns a pointer to the first character of a string of type String. This pointer can be passed to the wcscpy_s() function.
The wcscpy_s() function takes two parameters:
- Parameter of type wchar_t* (or wchar_t[]).This parameter is the result string of type wchar_t [].
- Parameter of type pin_ptr <type>. The reserved word pin_ptr means that the pointer needs to be fixed. This pointer is called “pointer of fixing“. In our case, this is a pointer to the first character of a string of type String. “Pointer of fixing” – it is an internal pointer that does not allow the object to move beyond the boundaries of allocated memory it. The word <type> in our case is replaced with <const wchar_t>.
To use the PtrToStringChars() and wcscpy_s() methods, you must connect the <vcclr.h> and <cstring> modules.
Example. Converting string src of type String to the string dest of type wchar_t.
#include <cstring> #include <vcclr.h> ... // converting from String to wchar_t String src = gcnew String("This is a text"); // source string src wchar_t dest[50]; // result string pin_ptr <const wchar_t> pdest = PtrToStringChars(src); // get a pointer to the result wcscpy_s(dest, pdest); // dest = "This is a text"
The PtrToStringChars() method takes an input parameter of type String.
Related topics
- Pointers. Basic concepts. Pointer types. Managed and unmanaged pointers
- Pointers. Unmanaged pointers. Operation on pointers. Pointer to type void. Memory allocation. A null pointer. The operation of getting the address &
- Pointers and arrays
- Pointers. Memory allocation for the pointer. Arrays of pointers to basic types, functions, structures, classes
- Pointers. Composite managed and native data types. Managed pointers (^) in CLR. Memory allocation. The ref and value qualifiers. Managed pointers to structures and classes
- Arrays. Two-dimensional arrays. Arrays of strings
- Examples of solving tasks using character strings
- Based types Visual C++