以下是我的代码的一部分。代码在数组listL中查找具有相同值的项组。理解我希望只需要main()。
#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;
}
我得到错误&#34;传递'strcat'的参数2使得指针来自整数而没有强制转换&#34;每次使用strcat。当我将snum定义为sum [256]时,是不是会占用?它是什么意思,我该如何解决?
我想要做的是追加字符串&#34; i_j _&#34;到组[leftIndex]中的项目。
答案 0 :(得分:4)
您将字符文字(包含在'
中)作为第二个参数传递给某些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]))
答案 1 :(得分:0)
强烈建议在启用所有警告的情况下进行编译。
使用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