strncpy

char * strncpy ( char * destination, const char * source, size_t num );

Parameters

destination
Pointer to the destination array where the content is to be copied.
source
C string to be copied.
num
Maximum number of characters to be copied from source.
size_t is an unsigned integral type.

Return Value

destination is returned.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* strncpy example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str1[]= "To be or not to be";
  char str2[40];
  char str3[40];

  /* copy to sized buffer (overflow safe): */
  strncpy ( str2, str1, sizeof(str2) );

  /* partial copy (only 5 chars): */
  strncpy ( str3, str2, 5 );
  str3[5] = '\0';   /* null character manually added */

  puts (str1);
  puts (str2);
  puts (str3);

  return 0;
}


Output:

To be or not to be
To be or not to be
To be 
創作者介紹
創作者 大玩家闖天涯 的頭像
大玩家闖天涯

大玩家闖天涯的部落格

大玩家闖天涯 發表在 痞客邦 留言(0) 人氣()