-3

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);  
Dunc
  • 7,688
  • 10
  • 31
  • 38

5 Answers5

3

*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];
hmjd
  • 120,187
  • 20
  • 207
  • 252
1

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.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
1

You are trying to strcat() with 0 as first parameter. *encryptedLine is encryptedLine[0] and you assigned {0} to it

Eregrith
  • 4,263
  • 18
  • 39
0

You have an array of char pointers

char *encryptedLine[255]

You haven't allocated any memory and assigned it to any of them. Crash.

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
0
/* 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.

display101
  • 2,035
  • 1
  • 14
  • 10