我不断收到一条错误消息“警告:指针和整数之间的比较”。我尝试使用 char* 但仍然遇到相同的错误。我想计算字符串中出现的逗号数量,并将出现的次数放入计数器中。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, char *argv[]) {
/*FILE *fp;
fp = fopen("csvTest.csv","r");
if(!fp){
printf("File did not open");
}*/
//char buff[BUFFER_SIZE];
char buff[100] = "1000,cap_sys_admin,cap_net_raw,cap_setpcap";
/*fgets(buff, 100, fp);
printf("The string length is: %lu\n", strlen(buff));
int sl = strlen(buff);*/
int count = 0;
int i;
for(i=0;buff[i] != 0; i++){
count += (buff[i] == ",");
}
printf("The number of commas: %d\n", count);
char *tokptr = strtok(buff,",");
char *csvArray[sl];
i = 0;
while(tokptr != NULL){
csvArray[i++] = tokptr;
tokptr …Run Code Online (Sandbox Code Playgroud)