Hedge
Hedge

Reputation: 16748

Copying part of char array into new char array fails

I want to copy a part of char array into a new one

void match(char* probe, char* pattern)
char* matchText;
//the char-array probe in this example is at least 12 characters long
//I'm only writing numbers in the strncopy-command to make it easier to understand
strncpy (matchText, probe + 5, 5 );

Upon running that the debugger quits with an error. What am I doing wrong?

Upvotes: 0

Views: 556

Answers (1)

Alok Save
Alok Save

Reputation: 206546

You need to allocate memory to matchText, what you have is just an pointer.
It must have enough memory allocated using malloc(since it is a pointer) to hold the string being copied in to it, or what you get is Undefined Behavior.

Upvotes: 3

Related Questions