簡體   English   中英

如何比較 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;
}


你有 go:

#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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM