Languages

Menu
Sites
Language
Failed to concate pointer variables

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.

Responses

5 Replies
Shaswati Saha

I've tried to do this in the following way. It worked without occuring an appcrash for me. Please have a look.

static void test() {

    char *finalFrame[25];
	finalFrame[6] = (char*) malloc(1);
	finalFrame[7] = (char*) malloc(1);

	strcpy(finalFrame[6], "0");
	strcpy(finalFrame[7], "3");


	char *id1 = (char*) malloc( sizeof(*finalFrame[6])+ 1 ); //0

	char *id0 = (char*) malloc( sizeof(*finalFrame[7]) + 1); //3


	strcpy(id1, finalFrame[6]);
	strcpy(id0, finalFrame[7]);

	dlog_print(DLOG_INFO, "My", " val %c %c ", *id0 , *id1);

	int a = (int) strlen(id0);
	int aa = (int) strlen(id1);

	char *idP = (char *)malloc(a + aa + 1);

	if(idP != NULL){
		strcpy(idP, id0);
		strcat(idP, id1);
		dlog_print(DLOG_INFO, "My", "idP : %s", idP);
	}else{
		dlog_print(DLOG_INFO, "My", "No memory");
	}

}

 

vrajesh s

Hi ,

Thanks for your feedback and code.

Code Works only assign static values in  

strcpy(finalFrame[6], "0");
strcpy(finalFrame[7], "3");

But it will not work when if i assign value dynamically through pinter variable app crash:  Plaease See code:

 

char *finalFrame[60];

//__bt_gatt_client_value_changed_cb listener
ret = bt_gatt_get_value(chr, &valueB, &len2); // chip output is valueB

if (ret != BT_ERROR_NONE) {
    	dlog_print(DLOG_INFO, LOG_TAG, "bt_gatt_get_value is failed : %d", ret);
	} else {
		dlog_print(DLOG_INFO, LOG_TAG,"bt_gatt_get_value for service is : %s, len: %d", valueB, len);
	}


for (int i = 0; i < len2; i++) {

		dlog_print(DLOG_INFO, LOG_TAG, "valueB %u", valueB[i]);

		finalFrame[counter] = (char*) malloc(1);
		<strong>strcpy(finalFrame[counter], valueB[i]); /</strong>/ not working, app crash
		finalFrame[counter] = valueB[i]; //working
		
		counter++;
	}

   for (int i = 0; i < counter; i++) {
    	dlog_print(DLOG_INFO, LOG_TAG, "FinalFrame[%d] %u", i, finalFrame[i]);

    if (i == 7) {
        char *id1 = (char*) malloc(sizeof(*finalFrame[6]) + 1); //0
    	char *id0 = (char*) malloc(sizeof(*finalFrame[7]) + 1); //3
        strcpy(id1, finalFrame[6]);
    		strcpy(id0, finalFrame[7]);
			dlog_print(DLOG_INFO, LOG_TAG, " val %c %c ",*id0, *id1);

            *int a = (int) strlen(id0);
    		 int aa = (int) strlen(id1);

			 char *idP = (char *) malloc(a + aa + 1);

			 if (idP != NULL) {
			    strcpy(idP, id0);
			    strcat(idP, id1);
			    dlog_print(DLOG_INFO, "My", "idP : %s", idP);
			 } else {
			    dlog_print(DLOG_INFO, "My", "No memory");
			 }
    }
}

 

Shaswati Saha

Try to use strcpy() in this way:

static void test2( char *a, char *b) {
        strcpy(a,b);
        dlog_print(DLOG_INFO, "My", "a = %s",a );
}
int main(int argc, char *argv[]) {

        char t[64] = {0,};
        strcpy(t,"52386");
        char val[64] = {0,};
        test2(val , t);
        dlog_print(DLOG_INFO, "My", "val = %s",val );

... ... ... 

}

hope it'll work.

vrajesh s

Hello, 

Thanks for reply, yes it works but static way, :(  not work when we use char pointer (dynamically add), please see below code.  find: Try1, Screenshot of error, Try2. 

 

Try 1: Create pointer variable and try to copy it, but failed

    		//char result3[5];
			char *result4=finalFrame[i];
			dlog_print(DLOG_INFO, LOG_TAG, "-----result4 : %d",result4); //display successfully.

			char t[64] = { 0,};
			strcpy(t, result4); // app crash here

			char val[64] = { 0, };
			strcpy(val, t);

			dlog_print(DLOG_INFO, LOG_TAG, "val = %s", val);

 

Screen shot: While debug error comes like can not access memory address, :

Try2: Assign memory to my array. finalfram[6] n finalfram[7], Is it proper ?

for (int i = 0; i < counter; i++) {
    dlog_print(DLOG_INFO, LOG_TAG, "FinalFrame[%d] %u", i, finalFrame[i]); // print successfully  3
       if (i == 7) {

            char *temp1[2];
    		temp1[0] = (char*) malloc(1);
			temp1[1] = (char*) malloc(1);

			//asign size to finalframe[6 , 7]
			char *ff1 = (char*) malloc(sizeof(*finalFrame[6]) + 1); //0
			char *ff2 = (char*) malloc(sizeof(*finalFrame[7]) + 1); //3

			ff1 = finalFrame[6];
			ff2 = finalFrame[7];

			strcpy(temp1[0], ff1);
			strcpy(temp1[1], ff2);

			char *id1 = (char*) malloc(sizeof(*temp1[0]) + 1); //0
			char *id0 = (char*) malloc(sizeof(*temp1[1]) + 1); //3

			id0 = temp1[0];
			id1 = temp1[1];

			strcpy(id0, temp1[0]);
			strcpy(id1, temp1[1]);

			dlog_print(DLOG_INFO, LOG_TAG, " val %c %c ", *id0, *id1);

			int a = (int) strlen(id0);
			int aa = (int) strlen(id1);

			char *idP = (char *) malloc(a + aa + 1);

			if (idP != NULL) {
				strcpy(idP, id0);
				strcat(idP, id1);
				dlog_print(DLOG_INFO, "My", "idP : %s", idP);
			} else {
				dlog_print(DLOG_INFO, "My", "No memory");
			}
    }//if
}//for loop

Note: According to: http://stackoverflow.com/questions/43638637/failed-to-concate-pointer-variables/43672132#43672132  my pointer variable is invalid. If my pointer variable is invalid then why result will print ? Please share your feedback on this.

Regards,

Vrajesh s.

 

Shaswati Saha

You've used assignment operator instead of using strcpy in some of the lines above. Try using strcpy in those cases and share the status here after that.