以下是我的代碼的一部分。代碼在數組listL中查找具有相同值的項目組。只有main()應該需要理解我的希望。strcat錯誤
#include <string.h>
#include <stdlib.h>
void append(char* s, char c)
{
int len = strlen(s);
s[len] = c;
s[len+1] = '\0';
}
int leftSame(int listL[4][4][2], int i, int j){
if(j==0){
return 0;
}
if(listL[i][j][0]==listL[i][j-1][0]){
return 1;
}
return 0;
}
int topSame(int listL[4][4][2], int i, int j){
if(i==0){
return 0;
}
if(listL[i][j][0]==listL[i-1][j][0]){
return 1;
}
return 0;
}
int realIndex(char group[64][256],int a){
while (strlen(group[a])<3){
a = atoi(group[a]);
}
return a;
}
int main(void)
{
int listL[4][4][2]= {{{0, 0}, {0, 0}, {0, 0}, {1, 0}},{{0, 0}, {0, 0}, {1, 0}, {0, 0}},{{0, 0}, {0, 0}, {0, 0}, {1, 0}},{{0, 0}, {0, 0}, {1, 0}, {1, 0}}};
char group[64][256];
int count = 0;
size_t i,j;
char snum[256];
int leftIndex=0;
int topIndex=0;
int a=0;
int A=0;
int B=0;
for (i = 0; i < 64; i++){
memset(group[i], 0, 256 + 1);// zero all memory in the list
}
for(i=0;i<sizeof(listL)/sizeof(listL[0]);i++){
for(j=0;j<sizeof(listL)/sizeof(listL[0]);j++){
A= leftSame(listL,i,j);
B=topSame(listL,i,j);
if(A && B){
leftIndex=realIndex(group,listL[i][j-1][1]);
topIndex=realIndex(group,listL[i-1][j][1]);
if (topIndex==leftIndex){
sprintf(snum, "%d", i);
strcat(group[leftIndex],snum);
strcat(group[leftIndex],'_');
sprintf(snum, "%d", j);
strcat(group[leftIndex],snum);
strcat(group[leftIndex],'_');
listL[i][j][1]=leftIndex;
}
}
}
}
return 0;
}
遇到錯誤「過客‘的strcat’的論點2從整數指針,未作類型轉換」每次使用的strcat的。當我將snum定義爲sum [256]時,是不是佔了?這是什麼意思,我該如何解決?
我想要做的是將字符串「i_j_」附加到組[leftIndex]中的項目。
'_'是ac字符,而不是字符串/字符數組。 – byako
發佈的代碼由於以下原因而無法編譯。建議使用所有編譯器警告。在許多其他的事情中,'#include'缺失,所以sprintf()沒有被定義,所以默認的(所有int)參數都是預期的 –
user3629249