我已经看到了有关指针和整数比较问题的各种解释,但是我对此仍然很困惑。自从我开始编码以来,我一直在为指针而苦恼,这似乎是我似乎无法掌握的。而且我发现自己经常遇到类似的问题。我获得了以下“警告”信号,我认为每种信号的解决方案都相似。谁能帮忙深入解释这个常见的指针问题?
错误
scanner.c: In function ‘delimiters’:
scanner.c:14:9: warning: comparison between pointer and integer [enabled by default]
if(ch == delimiter[i])
^
scanner.c: In function ‘operators’:
scanner.c:27:9: warning: comparison between pointer and integer [enabled by default]
if(ch == operator[i]){
^
scanner.c: In function ‘validIdentifier’:
scanner.c:43:2: warning: passing argument 1 of ‘delimiters’ makes pointer from integer without a cast [enabled by default]
if(id[0] == '0' || id[0] == '1' || id[0] == '2' || id[0] == '3' || id[0] == '4' || id[0] == '5' || id[0] == '6' || id[0] == '7' || id[0] == '8' || id[0] == '9' || delimiters(id[0]) == true || isupper(id[0]) )
^
scanner.c:8:6: note: expected ‘char *’ but argument is of type ‘char’
bool delimiters(char *ch){
^
scanner.c: In function ‘keywords’:
scanner.c:51:2: warning: passing argument 2 of ‘strcmp’ from incompatible pointer type [enabled by default]
if(!strcmp(ch, keyword)){
^
In file included from scanner.c:3:0:
/usr/include/string.h:140:12: note: expected ‘const char *’ but argument is of type ‘const char **’
extern int strcmp (const char *__s1, const char *__s2)
code.c
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include "token.h"
bool delimiters(char *ch){
int i, j;
int len = strlen(ch);
const char delimiter[10];
for(i = 0; i < len; i++){
if(ch == delimiter[i])
j = true;;
j = false;
}
return (j);
}
bool operators(char *ch){
int i, j;
int len = strlen(ch);
const char operator[10];
for(i = 0; i < len; i++){
if(ch == operator[i]){
j = true;
}
j = false;
}
return (j);
}
bool validIdentifier(char* id){
int len = strlen(id);
if((len == 0) || (len > 8)){
printf("Invalid identifier (string) length\n");
return (false);
}
if(id[0] == '0' || id[0] == '1' || id[0] == '2' || id[0] == '3' || id[0] == '4' || id[0] == '5' || id[0] == '6' || id[0] == '7' || id[0] == '8' || id[0] == '9' || delimiters(id[0]) == true || isupper(id[0]) )
return (false);
return (true);
}
bool keywords(char* ch){
const char* keyword[12];
if(!strcmp(ch, keyword)){
return (true);
}
return (false);
}
bool isInteger(char* ch){
int i;
int len = strlen(ch);
if((len == 0) || (len > 8)){
printf("Invalid number length\n");
return (false);
}
for(i = 0; i < len; i++){
if((ch[i] != '0' && ch[i] != '1' && ch[i] != '2' && ch[i] != '3' && ch[i] != '4' && ch[i] != '5' && ch[i] != '6' && ch[i] != '7' && ch[i] != '8' && ch[i] != '9' && ch[i] != '.') || (ch[i] == '-' && i > 0)){
return (false);
}
}
return (true);
}
char* sub(char* str, int l, int r){
int i;
char* subString = (char*)malloc(sizeof(char) * (r - l + 2));
for(i = l; i < r; i++){
subString[i - l] = str[i];
}
subString[r - l + 1] = '\0';
return (subString);
}