Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::strncpy

      From cppreference.com
      <cpp‎ |string‎ |byte
       
       
       
       
      Defined in header<cstring>
      char* strncpy(char* dest,constchar* src,std::size_t count);

      Copies at mostcount characters of the byte string pointed to bysrc (including the terminating null character) to character array pointed to bydest.

      Ifcount is reached before the entire stringsrc was copied, the resulting character array is not null-terminated.

      If, after copying the terminating null character fromsrc,count is not reached, additional null characters are written todest until the total ofcount characters have been written.

      If the strings overlap, the behavior is undefined.

      Contents

      [edit]Parameters

      dest - pointer to the character array to copy to
      src - pointer to the byte string to copy from
      count - maximum number of characters to copy

      [edit]Return value

      dest

      [edit]Example

      Run this code
      #include <cstring>#include <iostream> int main(){constchar* src="hi";char dest[6]={'a','b','c','d','e','f'};    std::strncpy(dest, src,5); std::cout<<"The contents of dest are: ";for(char c: dest){if(c)std::cout<< c<<' ';elsestd::cout<<"\\0"<<' ';}std::cout<<'\n';}

      Output:

      The contents of dest are: h i \0 \0 \0 f

      [edit]See also

      copies one string to another
      (function)[edit]
      copies one buffer to another
      (function)[edit]
      C documentation forstrncpy
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/byte/strncpy&oldid=152888"

      [8]ページ先頭

      ©2009-2025 Movatter.jp