Click to See Complete Forum and Search --> : Help with a char*problem


LHernandez
November 1st, 2002, 07:46 PM
I'm missing very basic regarding the char *. Why the program crashes when strcpy( result, Digits);

void Add(char * result)
{
char Digits[] = "1234567";

strcpy( result, Digits);
}

I'll appreciate your help.

Luis F Hernandez
EBS

Sput
November 1st, 2002, 07:54 PM
how to you call the function Add()? Is "result" pointing to something? Is it pointing to something big enough to hold the string "1234567"

Like: Is "result" also a character array of at least 7 in length.

JamesSchumacher
November 1st, 2002, 10:45 PM
The buffer needs to hold atleast 8 - 7 chars plus a null terminator, the '\0' char or 0.

doublehook
November 2nd, 2002, 11:07 PM
When you call the function "void Add(char *result)" you
must pass a pointer that realy points to some memory
allocated.

Example 1:

char memory[8];
Add(memory);

Example 2:

char *memory = new char[8];
Add(memory);

The size is greater than 7 because the string contains 7 digits and a null character that you don't see (7 + 1 = 8).