Strcat что делает
Перейти к содержимому

Strcat что делает

Функция strcat

Функция strcat() присоединяет к строке str1 копию строки str2 и завершает строку str1 нулевым символом. Конечный нуль-символ, первоначально завершающий строку str1 , перезаписывается первым символом строки str2 . Строка str2 при этом не изменяется. Если заданные массивы перекрываются, поведение функции strcat() не определено.

В версии С99 к параметрам str1 и str2 применен квалификатор restrict .

Функция strcat() возвращает значение указателя str1 .

Помните, что при выполнении операций с массивами символов контроль нарушения их границ не выполняется, поэтому программист должен сам позаботиться о достаточном размере массива str1 , позволяющем вместить как его исходное содержимое, так и содержимое массива str2 .

Пример

Данная программа дописывает первую строку, прочитанную из стандартного входного потока stdin , ко второй строке. Например, если пользователь введет строки

C Language: strcat function
(String Concatenation)

In the C Programming Language, the strcat function appends a copy of the string pointed to by s2 to the end of the string pointed to by s1. It returns a pointer to s1 where the resulting concatenated string resides.

Syntax

The syntax for the strcat function in the C Language is:

Parameters or Arguments

Returns

The strcat function returns a pointer to s1 (where the resulting concatenated string resides).

Required Header

In the C Language, the required header for the strcat function is:

  • Use the strcat function with caution as it is easy to concatenate more bytes into your variable using the strcat function, which can cause unpredictable behavior.

Applies To

In the C Language, the strcat function can be used in the following versions:

  • ANSI/ISO 9899-1990

strcat Example

Let’s look at an example to see how you would use the strcat function in a C program:

strcat, strcat_s

Because strcat needs to seek to the end of dest on each call, it is inefficient to concatenate many strings into one using strcat .

strcat_s is allowed to clobber the destination array from the last character written up to destsz in order to improve efficiency: it may copy in multibyte blocks and then check for null bytes.

The function strcat_s is similar to the BSD function strlcat , except that

  • strlcat truncates the source string to fit in the destination
  • strlcat does not perform all the runtime checks that strcat_s does
  • strlcat does not make failures obvious by setting the destination to a null string or calling a handler if the call fails.

Although strcat_s prohibits truncation due to potential security risks, it’s possible to truncate a string using bounds-checked strncat_s instead.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *