Results 1 to 6 of 6

Thread: Difference between memcpy() and strncpy() function in C

  1. #1
    Join Date
    Dec 2009
    Posts
    32

    Difference between memcpy() and strncpy() function in C

    Hello Friend,

    I am BSc(IT) student with lots of confusion about the various C functions. I want to be aware about the memcpy() and strncpy() function in C. I don't have any idea about their uses. What is the basic difference between memcpy() and strncpy()?. If you have sound knowledge about the memcpy() and strncpy() function in C then please let me know that. I would greatly appreciate your any help.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Difference between memcpy() and strncpy() function in C

    Hi friend,

    strcpy() function of C language is meant copy null terminated strings. See following it's details:
    • Used to copy each and every byte until it encounters a '#0'.
    • This function is not able to determine the data size of copied data.
    • This function is not bound with null terminated string.
    • This function of C can copy location of memory.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Difference between memcpy() and strncpy() function in C

    Hi,

    The memcpy() function of C programming is used to copy data bytes in memory. But the disadvantage of using memcpy() function is that it doesn't check the 'receiving memory area' overflow. following is the syntax for 'memcpy()' function.
    Syntax:
    void *memcpy (void *src, void *dest, size_t count);

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Difference between memcpy() and strncpy() function in C

    strncpy function copies 'n' bytes of data from one data string to another string. This function of C is includes in the 'string.h' library. This C function also copy all the count characters to the particular string of from.suppose there is less number of count characters then the remainder of this is can be padded with character known as '\0'. Please refer following it syntax:
    #include <string.h>

    char strncpy( const char *from, char *to, size_t count );

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: Difference between memcpy() and strncpy() function in C

    If you want to know about the memcpy() function of C then please study following it's example:
    #include<stdio.h>

    int main ()

    {

    char string01 [] = "chain of evidence";

    char string02 [80];

    memcpy (string02, string01, strlen (string01) 1);

    printf ( "string01:% snstring02:% sn", string01 , string02);

    memcpy (string01, "", 1);

    printf ( "string01:% sn", string01);

    return 0 ;

    }

  6. #6
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Difference between memcpy() and strncpy() function in C

    Difference between memcpy() and strncpy() function:
    1. strcpy() function of C is related to the copy of null terminated strings.
    2. memcpy() function of C is not related to the null terminated strings.
    3. strcpy() function can identify the size of data.
    4. memcpy() function can not identify the size of data.

Similar Threads

  1. c# function equivalent to gettime function in javascript
    By Omaar in forum Software Development
    Replies: 4
    Last Post: 10-03-2010, 10:44 PM
  2. How does abstract function differs from virtual function?
    By Maddox G in forum Software Development
    Replies: 5
    Last Post: 29-01-2010, 11:32 AM
  3. Replies: 2
    Last Post: 28-02-2009, 10:32 PM
  4. What is the difference between a function and a subroutine?
    By Pooja in forum Software Development
    Replies: 5
    Last Post: 28-02-2009, 06:43 PM
  5. Difference between Sub and Function in Visual Basic and VB.Net
    By Bellamy in forum Software Development
    Replies: 3
    Last Post: 21-01-2009, 10:11 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,521,444.95785 seconds with 16 queries