strncpy ในภาษา C, C++

6 Sep 2015

Share to:

strncpy เป็น function สําหรับ copy string จากตัวแปรต้นทางไปใส่ในตัวแปรปลายทาง

รูปแบบการใช้

char *strncpy(char *dest, const char *src, size_t n)

 Parameter

  • dest —String ปลายทาง
  • src —String ต้นทาง
  • n —จํานวนตัวอักษรของต้นทางที่ต้องการ copy

Example

#include <stdio.h>
#include <string.h>

int main()
{
 char src[] = "This is thitiBlog.com";
 char dest[50];

 memset(dest, '\0', sizeof(dest));
 strncpy(dest, src, 10);

 printf("Final copied string : %s\n", dest);

 return(0);
}

Output:

Final copied string : This is th

Copyright © 2019 - 2024 thiti.dev |  v1.39.0 |  Privacy policy | 

Build with ❤️ and Astro.

Github profile   Linkedin profile   Instagram   X profile   Youtube channel   Telegram   Email contact   วงแหวนเว็บ