Failed to concate pointer variables

Failed to concate pointer variables

BY 27 Apr 2017 Native Application Development

I want to concat two pointer variable. try different apporch but failed, No Resource found – No address found error comes.

FYI: Tizen Wearable SDK 3.0 Samsung Gear S2 watch

I am parsing frames which comes from BLE chip. i have to decode it and get information then display it on UI. so choose array.

Try1:

char *idP;

  //finalFrame is pointer array. 
  char *id1 = finalFrame[6]; //0
  char *id0 = finalFrame[7]; //3 
  dlog_print(DLOG_INFO, LOG_TAG, "id0 : %d", id0); // output: id1 : 0 
  dlog_print(DLOG_INFO, LOG_TAG, "id1 : %d", id1); // output: id1 : 3 

   if((idP = (char *)malloc(strlen(id0) + strlen(id1) + 1)) != NULL){ // appcrash here
                *idP='\0';
                strcpy(idP, id1);
                strcat(idP, id0);
                dlog_print(DLOG_INFO, LOG_TAG, "-----idP %s",idP);
            }else{
                dlog_print(DLOG_INFO, LOG_TAG, "No memory");
            }

 

Try2:

 

char *idP=concat(id1,id0);

dlog_print(DLOG_INFO, LOG_TAG, "-----id : %s", idP); // but failed

//method
char * concat(char *str1, char *str2) {
char *conc = str1;

while (*str1 != '\0')
    str1++;

*str1 = ' ';
++str1;

while (*str2 != '\0') {
    *str1 = *str2;
    str1++, str2++;
}

*str1 = '\0';
//printf("Concatenated String:\n");
//puts(conc);
dlog_print(DLOG_INFO, LOG_TAG, "%s", conc);
return conc;

}

 

Try3:

char *prefixAry[2];
char *prefix = malloc(sizeof(char) * strlen(prefixAry) + 1);//+1 for the zero-terminator;
             *prefix = '\0';
             strcat(prefix, prefixAry[0]); //any value set
             strcat(prefix, prefixAry[1]); //any value set
             dlog_print(DLOG_INFO, LOG_TAG, "-----Prefix : %s", prefix);

Am i doing right way ? Please help.

Link: http://stackoverflow.com/questions/43638637/failed-to-concate-pointer-variables

 

Thanks in advance. Regards.

Written by