strcat 错误

Errors with strcat

以下是我的部分代码。该代码在数组 listL 中查找具有相同值的项目组。我希望只需要 main() 来理解。

#include <string.h>
#include <stdlib.h>

void append(char* s, char c)
{
        int len = strlen(s);
        s[len] = c;
        s[len+1] = '[=10=]';
}

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 都会收到错误 "passing argument 2 of ‘strcat’ makes pointer from integer without a cast"。我把snum定义成sum[256]的时候不考虑cast吗?这是什么意思,我该如何解决?

我想做的是将字符串 "i_j_" 附加到组 [leftIndex] 中的项目。

您将字符文字(包装在 ' 中)作为第二个参数传递给某些 strcat,例如

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]))

强烈建议在启用所有警告的情况下进行编译。

对于 gcc 使用

'-Wall -Wextra -pedantic' 

最低

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