Hamid Hamid - 1 year ago 210
C Question

strcpy and strcat in Keil C compiler

I used this code to print some string,but it does not print any thing.
What is the problem?

char* getNotFilledEncryptionParams(void)
char* nofilledStr;
char tmp[3];
const char * arr[]= {" P,"," Q,"," A,"," B,"," C,"," R,"," S0,","S1,","S2,","F1,","G1"};

if(filledParams[i] == 0)

return nofilledStr;


int main(void){

char *remaining;

remaining = getNotFilledEncryptionParams();
printf("\r\n Remaining item:%s",remaining);


I think the problem is in
const char * arr[]
and I changed it,but the problem remains.

Answer Source
  1. You didn't allocate any memory for noFilledStr, so its value is indeterminate and strcat(noFilledStr, tmp) is undefined.

    Use malloc to allocate memory and initialize noFilledStr with the returned pointer:

    char* noFilledStr = malloc(number_of_bytes);
  2. The strings in arr are char[4], not char[3] (do not forget the null byte!). tmp is too small to hold them, so strcpy(tmp, arr[i]) writes out of bounds.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download