#include<stdio.h>
int main()
{
unsigned char a[3];
unsigned char b[3];
int *l[3]=NULL;
int i = 0;
a[0]=0;
a[1]=1;
a[2]=2;
b[0]=3;
b[1]=4;
b[2]=5;
l[0]=&a;
l[1]=&b;
if(strcmp(l[0],l[1])==0) {
printf("Compared not same");
}
return 0;
}
我想将数组存储在数组“l”中。并且要比较存储在索引0和索引1处的数组。 我收到了错误。请帮忙。
答案 0 :(得分:1)
#include<stdio.h>
int main()
{
unsigned char a[3];
unsigned char b[3];
unsigned char *l[2];
a[0] = 0; a[1] = 1; a[2] = 2;
b[0] = 3; b[1] = 4; b[2] = 5;
l[0] = a; l[1] = b;
if(strncmp(l[0], l[1], 3) != 0) {
printf("Compared not same");
}
return 0;
}
答案 1 :(得分:1)
#include<stdio.h>
int main()
{
unsigned char a[3];
unsigned char b[3];
unsigned char *l[2];
a[0] = '3'; a[1] = '4'; a[2] = '\0';
b[0] = '3'; b[1] = '4'; b[2] = '\0';
l[0] = a; l[1] = b;
if(strcmp(l[0], l[1]) != 0) {
printf("Compared not same");
} else {
printf("Compared same");
}
return 0;
}
答案 2 :(得分:0)
int main()
{
char a[3] = {1, 1, 2};
char b[3] = {1, 4, 5};
char *l[2]= {a, b};
printf( (strncmp(l[0], l[1], 3)==0) ? "Compared are equal" : "Compared are not equal");
return 0;
}
请注意,0是行的结尾,因此chars [0,1,2]和[0,5,5]的数组相等。此外,如果您在此类数组中没有0,则可能会导致错误,因为它将是无限数组,并将尝试从内存中获取不适用于此数组的值。你真的应该从任何关于该语言基础知识的好的c / c ++书开始。
答案 3 :(得分:0)
不确定在数组中存储数组,但您应该使用memcmp
代替strcmp
来比较数组(因为strcmp
比较字符串,而您的数组不是字符串。)
unsigned char a[3];
unsigned char b[3];
unsigned char *l[2]; // note: corrected a few errors in this line
int i = 0;
a[0]=0;
a[1]=1;
a[2]=2;
b[0]=3;
b[1]=4;
b[2]=5;
l[0]=a;
l[1]=b;
if(memcmp(l[0],l[1], sizeof(a))==0) { // note: have to specify the length
printf("Compared same"); // note: reveresed the logic
}