How to copy char pointer to char array?

So, I have a pointer to a char array:

temporaryVariable->arrayOfElements; // arrayOfElements is char*

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

char stringArray[MAXIMUM_LINE_LENGTH + 1];

How can i do this?

+4
source share
2 answers

Use strncpy:

strncpy(stringArray, temporaryVariable->arrayOfElements, sizeof(stringArray));
stringArray[sizeof(stringArray) - 1] = '\0';
+4
source

this code is also ok.

snprintf(stringArray,MAXIMUM_LINE_LENGTH + 1,"%s",temporaryVariable->arrayOfElements);
0
source

All Articles