My application falls over when I make the following call to strcat any ideas why?
char *encryptedLine[255] = {0};
char encryptedLString[8];
sprintf(encryptedLString, "%08lX", L);
strcat(*encryptedLine, encryptedLString);
My application falls over when I make the following call to strcat any ideas why?
char *encryptedLine[255] = {0};
char encryptedLString[8];
sprintf(encryptedLString, "%08lX", L);
strcat(*encryptedLine, encryptedLString);
*encryptedLine
is NULL pointer: it needs to point a char
buffer.
*encyptedLine
is equivalent to encryptedLine[0]
, which is the first entry in the array of char*
encryptedLine
:
char *encryptedLine[255] = { 0 };
which is a NULL pointer.
To fix, either change to:
char encryptedLine[255] = { 0 };
strcat(encryptedLine, encryptedString);
or:
encryptedLine[0] = malloc(255);
strcat(*encryptedLine, encryptedString);
I think you also need to increase the size of encryptedLString
by 1 as:
sprintf(encryptedLString, "%08lX", L);
will attempt to write 9 characters: 8 specified by the format plus 1 for the null terminator:
char encryptedLString[9];
encryptedLine is an array of 255 pointers initialized to NULL. *encryptedLine is a NULL pointer. You are passing a NULL pointer as the first argument to strcat, so it fails.
You are trying to strcat()
with 0 as first parameter. *encryptedLine
is encryptedLine[0]
and you assigned {0}
to it
You have an array of char
pointers
char *encryptedLine[255]
You haven't allocated any memory and assigned it to any of them. Crash.
/* LINE 1*/ char *encryptedLine[255] = {0};
char encryptedLString[8];
sprintf(encryptedLString, "%08lX", L);
strcat(*encryptedLine, encryptedLString);
Look at LINE1, it allocates an array of 255 pointers to char that are all initialised to 0, then you do a strcat, which will try to write the value from encryptedLString into the location pointed to by the pointer *encryptedLine which is NULL, which isn't allowed.
Just allocate some memory for encryptedLine's pointers, and this program won't crash.