如何在 C 中比较 char 和 char *
How to compare char and char * in C
我不确定为什么这不起作用,它不打印任何内容。 csvArry 有 3 个元素,capList 有 4 个元素。我想搜索 capList 以查看其中是否有与 csvArray 中的元素匹配的元素。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, char *argv[]) {
char *csvArray[] = {"1000", "CAP_SYS_ADMIN", "CAP_SYS_RAW"};
char *capList[] = {"CAP_SYS_SETFCAP", "CAP_SYS_SETPCAP", "CAP_SYS_ADMIN", "CAP_SYS_RAW"};
int i = 0;
int j;
while(i<3){
for(j=0;j<4;j++){
if(strcmp(csvArray[i],capList[j]) == 0){
printf("Match");
}
}
i++;
}
return 0;
}
好了:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
char *csvArray[] = {"1000", "CAP_SYS_ADMIN", "CAP_SYS_RAW"};
char *capList[] = {"CAP_SYS_SETFCAP", "CAP_SYS_SETPCAP", "CAP_SYS_ADMIN", "CAP_SYS_RAW"};
int size = sizeof(csvArray) / sizeof(csvArray[0]);
int sizeOfList = sizeof(capList) / sizeof(capList[0]);
for (int i = 0; i < size; i++) {
for (int j = 0; j < sizeOfList; j++) {
if (strcmp(csvArray[i], capList[j]) == 0) {
printf("%s Match\n", csvArray[i]);
}
}
}
return 0;
}
该程序试图找出哪些元素是两者共有的,并打印出哪些是共有的。
Output
CAP_SYS_ADMIN Match
CAP_SYS_RAW Match
我不确定为什么这不起作用,它不打印任何内容。 csvArry 有 3 个元素,capList 有 4 个元素。我想搜索 capList 以查看其中是否有与 csvArray 中的元素匹配的元素。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, char *argv[]) {
char *csvArray[] = {"1000", "CAP_SYS_ADMIN", "CAP_SYS_RAW"};
char *capList[] = {"CAP_SYS_SETFCAP", "CAP_SYS_SETPCAP", "CAP_SYS_ADMIN", "CAP_SYS_RAW"};
int i = 0;
int j;
while(i<3){
for(j=0;j<4;j++){
if(strcmp(csvArray[i],capList[j]) == 0){
printf("Match");
}
}
i++;
}
return 0;
}
好了:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
char *csvArray[] = {"1000", "CAP_SYS_ADMIN", "CAP_SYS_RAW"};
char *capList[] = {"CAP_SYS_SETFCAP", "CAP_SYS_SETPCAP", "CAP_SYS_ADMIN", "CAP_SYS_RAW"};
int size = sizeof(csvArray) / sizeof(csvArray[0]);
int sizeOfList = sizeof(capList) / sizeof(capList[0]);
for (int i = 0; i < size; i++) {
for (int j = 0; j < sizeOfList; j++) {
if (strcmp(csvArray[i], capList[j]) == 0) {
printf("%s Match\n", csvArray[i]);
}
}
}
return 0;
}
该程序试图找出哪些元素是两者共有的,并打印出哪些是共有的。
Output
CAP_SYS_ADMIN Match
CAP_SYS_RAW Match