What is the difference between string and array

What is the difference between string and array in C

  • Arrays in C can store any data type whereas Strings can store only char data.
  • C String must be terminated by a null character(‘\0’) whereas there is no such restrictions in Array.
  • An array can be of any length, unless we don’t specify the length of array there is no way to determine the end of array whereas a String is terminated by a null character(‘\0’) which represents the end of string.

What is the difference between strcpy() and strncpy() String functions in C

The function char strcpy(char *destination, const char *source); copies the string pointed by source into the string pointed by destination including the null character(‘\0’).
The function char *strncpy(char *destination, const char *source, size_t n); copies the first n characters from the string pointed by source to string pointed by destination. If the length of source string is less than n, then destination string is padded with zeros until a total of n characters have been written into it.

What is the difference between memcpy() and memmove() String functions in C

The function void *memcpy(void *destination, const void *source, size_t n); copies first n bytes from memory location pointed by source to memory location pointed by destination. It does the binary copy of the data. It always copies exactly num bytes without checking for terminating null character(‘\0’) in source.

The function void *memmove ( void *destination, const void *source, size_t n); copies first n bytes from memory location pointed by source to memory location pointed by destination. It does the binary copy of the data. We can copy overlapping source and destination memory locations using memmove function.
The main difference between memcpy and memmove is the memory handling when source and destination overlaps.

Leave a Comment