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?

letter Q
  • 14,735
  • 33
  • 79
  • 118

2 Answers2

6

Use strncpy:

strncpy(stringArray, temporaryVariable->arrayOfElements, sizeof(stringArray));
stringArray[sizeof(stringArray) - 1] = '\0';
user4815162342
  • 141,790
  • 18
  • 296
  • 355
1

this code is also ok.

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