5

So I have a pointer to a char array:

temporaryVariable->arrayOfElements; // arrayOfElements is char*

I would like to copy into my char array declared with square brackets:

char stringArray[MAXIMUM_LINE_LENGTH + 1];

How can I do this?

Wang-Zhao-Liu Q
  • 13,555
  • 29
  • 73
  • 112

2 Answers2

5

Use strncpy:

strncpy(stringArray, temporaryVariable->arrayOfElements, sizeof(stringArray));
stringArray[sizeof(stringArray) - 1] = '\0';
user4815162342
  • 124,516
  • 15
  • 228
  • 298
1

this code is also ok.

snprintf(stringArray,MAXIMUM_LINE_LENGTH + 1,"%s",temporaryVariable->arrayOfElements);
freedoo
  • 691
  • 1
  • 5
  • 12