重疊

2016-11-07 106 views
0
char pt[116]; 

PT是包含3份分離由消息「:」和最後部分是隨機值重疊

我有存儲在指針的每個值

char* x = strtok(pt, ":") ; 
char* y = strtok(NULL, ":") ; 
char* z = strtok(NULL, ":") ; 

printf("x:%s \n",x); 
printf("y:%s \n",y); 
printf("z:%s \n",z); 

和後來我需要一些字符的第一部分

memcpy(x,strcat(x,".pem"),strlen(x)+4)); //==>this line cause the problem coz 
x[strlen(x)]='\0'; 
printf("%s\n",x); 
memcpy(y,y,strlen(y)+strlen(x)+4); 
printf("y is :%s\n",y); // here the output is wrong it prints the pem the added part to x 

那麼,什麼是建議添加.pem到x沒有重疊memcyp?

+0

你能解釋一下你在導致問題的行中意味着什麼嗎? –

+0

這段代碼沒有意義。 – OldProgrammer

+0

你想做什麼? 'strcat(x,something)'會返回'x'。然後嘗試將'x'複製到'x',但可能使用太多的字節。另一個問題是,我們不知道'strlen(x)'是否意味着調用'strcat'之前或之後的長度,因爲評估參數的順序沒有定義。 –

回答

1

的strtok

這個函數返回一個指向字符串中找到的最後一個令牌。如果沒有令牌可供檢索,則返回空指針。

當您使用strcat到廣告.pem字符串(x)要覆蓋y部分的第一部分。

您應該隔離不同數組(c-string)中的每個字符串並更改而不是原始字符串。

// Create a new container for x. Note: +1 is for the c-string null terminator 
x_new[strlen(x)+4+1]; 

strcpy(x_new, x); 
strcat(x_new, ".pem");