Skip to content

Commit ad8ac11

Browse files
author
Colin Robertson
authored
Merge pull request #3173 from MicrosoftDocs/master637581656630745476
Repo sync for protected CLA branch
2 parents 43703f0 + 1e8960a commit ad8ac11

36 files changed

+10564
-10569
lines changed

docs/atl-mfc-shared/cstring-operations-relating-to-c-style-strings.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ ms.date: "11/04/2016"
55
helpviewer_keywords: ["CString objects, basic operations", "MFC [C++], string handling class", "string conversion [C++], C-style strings", "strings [C++], string operations", "standard run-time library string functions", "null values, Null-terminated string conversion", "string functions", "strings [C++], in C", "string arguments", "C-style strings", "strings [C++], class CString", "casting CString objects"]
66
ms.assetid: 5048de8a-5298-4891-b8a0-c554b5a3ac1b
77
---
8-
# CString Operations Relating to C-Style Strings
8+
# `CString` Operations Relating to C-Style Strings
99

10-
A [CString](../atl-mfc-shared/using-cstring.md) object contains character string data. `CString` inherits the set of the [methods and operators](../atl-mfc-shared/reference/cstringt-class.md) that are defined in the class template [CStringT](../atl-mfc-shared/reference/cstringt-class.md) to work with string data. (`CString` is a **`typedef`** that specializes `CStringT` to work with the kind of character data that `CString` supports.)
10+
A [`CString`](../atl-mfc-shared/using-cstring.md) object contains character string data. `CString` inherits the set of the [methods and operators](../atl-mfc-shared/reference/cstringt-class.md) that are defined in the class template [`CStringT`](../atl-mfc-shared/reference/cstringt-class.md) to work with string data. (`CString` is a **`typedef`** that specializes `CStringT` to work with the kind of character data that `CString` supports.)
1111

1212
`CString` does not store character data internally as a C-style null-terminated string. Instead, `CString` tracks the length of character data so that it can more securely watch the data and the space it requires.
1313

@@ -17,23 +17,23 @@ A [CString](../atl-mfc-shared/using-cstring.md) object contains character string
1717

1818
- [Working with standard run-time library string functions](#_core_working_with_standard_run.2d.time_library_string_functions)
1919

20-
- [Modifying CString contents directly](#_core_modifying_cstring_contents_directly)
20+
- [Modifying `CString` contents directly](#_core_modifying_cstring_contents_directly)
2121

22-
- [Using CString objects with variable argument functions](#_core_using_cstring_objects_with_variable_argument_functions)
22+
- [Using `CString` objects with variable argument functions](#_core_using_cstring_objects_with_variable_argument_functions)
2323

24-
- [Specifying CString formal parameters](#_core_specifying_cstring_formal_parameters)
24+
- [Specifying `CString` formal parameters](#_core_specifying_cstring_formal_parameters)
2525

26-
## <a name="_core_using_cstring_as_a_c.2d.style_null.2d.terminated_string"></a> Using CString as a C-Style Null-Terminated String
26+
## <a name="_core_using_cstring_as_a_c.2d.style_null.2d.terminated_string"></a> Using `CString` as a C-Style Null-Terminated String
2727

28-
To use a `CString` object as a C-style string, cast the object to LPCTSTR. In the following example, the `CString` returns a pointer to a read-only C-style null-terminated string. The `strcpy` function puts a copy of the C-style string in the variable `myString`.
28+
To use a `CString` object as a C-style string, cast the object to `LPCTSTR`. In the following example, the `CString` returns a pointer to a read-only C-style null-terminated string. The `strcpy` function puts a copy of the C-style string in the variable `myString`.
2929

3030
```cpp
3131
CString aCString = "A string";
3232
char myString[256];
3333
strcpy(myString, (LPCTSTR)aCString);
3434
```
3535
36-
You can use `CString` methods, for example, `SetAt`, to modify individual characters in the string object. However, the LPCTSTR pointer is temporary and becomes invalid when any change is made to `CString`. The `CString` can also go out of scope and be automatically deleted. We recommend that you get a fresh LPCTSTR pointer of a `CString` object every time that you use one.
36+
You can use `CString` methods, for example, `SetAt`, to modify individual characters in the string object. However, the `LPCTSTR` pointer is temporary and becomes invalid when any change is made to `CString`. The `CString` can also go out of scope and be automatically deleted. We recommend that you get a fresh `LPCTSTR` pointer of a `CString` object every time that you use one.
3737
3838
Sometimes you may require a copy of `CString` data to modify directly. Use the more secured function `strcpy_s` (or the Unicode/MBCS-portable `_tcscpy_s`) to copy the `CString` object into a separate buffer. This is where characters can be safely modified, as shown by the following example.
3939
@@ -46,35 +46,35 @@ Sometimes you may require a copy of `CString` data to modify directly. Use the m
4646
4747
You should be able to find a `CString` method to perform any string operation for which you might consider using the standard C run-time library string functions such as `strcmp` (or the Unicode/MBCS-portable `_tcscmp`).
4848
49-
If you must use the C run-time string functions, you can use the techniques described in _core_using_cstring_as_a_c.2d.style_null.2d.terminated_string. You can copy the `CString` object to an equivalent C-style string buffer, perform your operations on the buffer, and then assign the resulting C-style string back to a `CString` object.
49+
If you must use the C run-time string functions, you can use the techniques described in [Using `CString` as a C-style null-terminated string](#_core_using_cstring_as_a_c.2d.style_null.2d.terminated_string). You can copy the `CString` object to an equivalent C-style string buffer, perform your operations on the buffer, and then assign the resulting C-style string back to a `CString` object.
5050
51-
## <a name="_core_modifying_cstring_contents_directly"></a> Modifying CString Contents Directly
51+
## <a name="_core_modifying_cstring_contents_directly"></a> Modifying `CString` Contents Directly
5252
5353
In most situations, you should use `CString` member functions to modify the contents of a `CString` object or to convert the `CString` to a C-style character string.
5454
5555
There are some situations where it makes sense to directly modify the `CString` contents, for example, when you work with operating-system functions that require a character buffer.
5656
5757
The `GetBuffer` and `ReleaseBuffer` methods offer access to the internal character buffer of a `CString` object and let you modify it directly. The following steps show how to use these functions for this purpose.
5858
59-
### To use GetBuffer and ReleaseBuffer to access the internal character buffer of a CString object
59+
### To use `GetBuffer` and `ReleaseBuffer` to access the internal character buffer of a `CString` object
6060
6161
1. Call `GetBuffer` for a `CString` object and specify the length of the buffer you require.
6262
6363
1. Use the pointer returned by `GetBuffer` to write characters directly into the `CString` object.
6464
6565
1. Call `ReleaseBuffer` for the `CString` object to update all the internal `CString` state information, for example, the length of the string. After you modify the contents of a `CString` object directly, you must call `ReleaseBuffer` before you call any other `CString` member functions.
6666
67-
## <a name="_core_using_cstring_objects_with_variable_argument_functions"></a> Using CString Objects with Variable Argument Functions
67+
## <a name="_core_using_cstring_objects_with_variable_argument_functions"></a> Using `CString` Objects with Variable Argument Functions
6868
6969
Some C functions take a variable number of arguments. A notable example is `printf_s`. Because of the way this kind of function is declared, the compiler cannot be sure of the type of the arguments and cannot determine which conversion operation to perform on each argument. Therefore, it is essential that you use an explicit type cast when passing a `CString` object to a function that takes a variable number of arguments.
7070
71-
To use a `CString` object in a variable argument function, explicitly cast the `CString` to an LPCTSTR string, as shown in the following example.
71+
To use a `CString` object in a variable argument function, explicitly cast the `CString` to an `LPCTSTR` string, as shown in the following example.
7272
7373
[!code-cpp[NVC_ATLMFC_Utilities#190](../atl-mfc-shared/codesnippet/cpp/cstring-operations-relating-to-c-style-strings_2.cpp)]
7474
75-
## <a name="_core_specifying_cstring_formal_parameters"></a> Specifying CString Formal Parameters
75+
## <a name="_core_specifying_cstring_formal_parameters"></a> Specifying `CString` Formal Parameters
7676
77-
For most functions that need a string argument, it is best to specify the formal parameter in the function prototype as a **`const`** pointer to a character (`LPCTSTR`) instead of a `CString`. When a formal parameter is specified as a **`const`** pointer to a character, you can pass either a pointer to a TCHAR array, a literal string [`"hi there"`], or a `CString` object. The `CString` object will be automatically converted to an LPCTSTR. Any place you can use an LPCTSTR, you can also use a `CString` object.
77+
For most functions that need a string argument, it is best to specify the formal parameter in the function prototype as a **`const`** pointer to a character (`LPCTSTR`) instead of a `CString`. When a formal parameter is specified as a **`const`** pointer to a character, you can pass either a pointer to a `TCHAR` array, a literal string [`"hi there"`], or a `CString` object. The `CString` object will be automatically converted to an `LPCTSTR`. Any place you can use an `LPCTSTR`, you can also use a `CString` object.
7878
7979
You can also specify a formal parameter as a constant string reference (that is, `const CString&`) if the argument will not be modified. Drop the **`const`** modifier if the string will be modified by the function. If a default null value is desired, initialize it to the null string [`""`], as shown below:
8080
@@ -85,4 +85,4 @@ For most function results, you can simply return a `CString` object by value.
8585
## See also
8686
8787
[Strings (ATL/MFC)](../atl-mfc-shared/strings-atl-mfc.md)<br/>
88-
[CString Argument Passing](../atl-mfc-shared/cstring-argument-passing.md)
88+
[`CString` Argument Passing](../atl-mfc-shared/cstring-argument-passing.md)

0 commit comments

Comments
 (0)