2015-05-17 46 views
0

以下是我的代碼的一部分。代碼在數組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]中的項目。

+2

'_'是ac字符,而不是字符串/字符數組。 – byako

+0

發佈的代碼由於以下原因而無法編譯。建議使用所有編譯器警告。在許多其他的事情中,'#include '缺失,所以sprintf()沒有被定義,所以默認的(所有int)參數都是預期的 – user3629249

回答

4

你通過字符文字(包裹在')作爲第二個參數的一些strcat S的像

strcat(group[leftIndex],'_'); 

strcat預計第二個參數(以及它的第一個參數)爲char*類型的,而不是char,它們都需要NUL終止。這就是爲什麼抱怨者抱怨。

使用字符串文字而不是文字的一個字符來解決這個問題:

strcat(group[leftIndex],"_"); 

memset(group[i], 0, 256 + 1); 

有一個錯誤的了。使用

memset(group[i], 0, 256); 

或更好

memset(group[i], 0, sizeof(group[i])) 
+0

另外,似乎'group'永遠不會被初始化。 –

+1

@iharob,'memset(group [i],0,256 + 1)'?雖然它應該是'memset(group [i],0,256)'或更好'memset(group [i],0,sizeof(group [i])' –

+0

嘿,那真的!謝謝,我是 – user2998454

0

強烈建議啓用所有警告編譯。

海合會使用

'-Wall -Wextra -pedantic' 

爲minumum

remember that code formatting/style is for human readability 
so use white space, both vertically around code blocks 
and horizontally between 'tokens' 

這裏是main()函數的修改清單,這完全編譯

加上缺少的頭文件:

#include <stdio.h> // sprintf() 

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); 
        sprintf(snum, "%u", (unsigned)i); 

        strcat(group[leftIndex], snum); 
        //strcat(group[leftIndex],'_'); 
        strcat(group[leftIndex],"_"); 

        //sprintf(snum, "%d", j); 
        sprintf(snum, "%u", (unsigned)j); 

        strcat(group[leftIndex], snum); 

        //strcat(group[leftIndex],'_'); 
        strcat(group[leftIndex], "_"); 

        listL[i][j][1] = leftIndex; 
       } 
      } 
     } 

    } 
    return 0; 
} // end function: main