How can I overwrite an array of chars (AKA a string), with a new array of chars in C?

Matt picture Matt · Apr 20, 2011 · Viewed 22.1k times · Source
int main(void) {
  ...
  char A[32] = "00000000000000001111111111111110"; 
  ...
  A = "11111111111111111111111111111111";
}

This is erroneous c-code for what I want to do. I want the string in memory to be overwritten with a new string of the same length. I keep getting incompatible types -like errors.

Answer

Damian Carrillo picture Damian Carrillo · Apr 20, 2011

Use strncpy:

char chararray[6];
(void)strncpy(chararray, "abcdefgh", sizeof(chararray));