This blog is under construction

Sunday, 29 April 2012

memmove example in C

Header file:
    string.h

Synopsis:
     void *memmove(void *dest, const void *src, size_t num);

Description:
   It copies num bytes from src to dest and returns dest.  One advantage with memmove is that it works even if the objects overlap.


memmove function C example:


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

  struct student {
        char name[100];
        char course[100];
        int duration;
  };

  int main() {
        struct student *from, *to;
        from = (struct student *)malloc(sizeof(struct student));
        to = (struct student *)malloc(sizeof(struct student));
        printf("Enter the student name:");
        fgets(from->name, 30, stdin);
        printf("Enter the course name:");
        fgets(from->course, 30, stdin);
        printf("Enter the duration:");
        scanf("%d", &from->duration);
        to = (struct student *)memmove(to, from, sizeof(struct student));
        if (to == NULL)
                printf("Error\n");
        else {
                printf("Output:\n");
                printf("Name: %s", to->name);
                printf("Course: %s", to->course);
                printf("Duration: %d\n", to->duration);
        }
        return 0;

  }



  Output:
  jp@jp-VirtualBox:~/cpgms/chap5$ ./a.out
  Enter the student name:Stuart
  Enter the course name:BE
  Enter the duration:4
  Output:
  Name: Stuart
  Course: BE
  Duration: 4

1 comment: