从char到const char*的转换无效

Conversion not valid from char to const char*

本文关键字:char 转换 无效 const      更新时间:2023-10-16

我编写了这个程序,用于在程序作为文件提供给lex时查找lex中的关键字和标识符

#include<iostream>
#include<stdio.h>
#include<ctype.h>
#include<string.h>
using namespace std;
void keyw(char *p);
int i=0,id=0,kw=0,num=0,op=0,n=0;
char s[32];
char keys[32][10]={"auto","break","case","char","const","continue","default",
"do","double","else","enum","extern","float","for","goto",
"if","int","long","register","return","short","signed",
"sizeof","static","struct","switch","typedef","union",
"unsigned","void","volatile","while"};
int main()
{
    char ch,str[25];
    char seps[]=" tn,;(){}[]#"<>";
    char oper[]="!%^&*-+=~|.<>/?";
    int j;
    char fname[50];
    FILE *f1;
    printf("enter file pathn");
    scanf("%s",fname);
    f1 = fopen(fname,"r");
    if(f1==NULL)
    {
        printf("file not found");
        return 0;
    }
    while((ch=fgetc(f1))!=EOF)
    {
        for(j=0;j<=14;j++)
        {
            if(ch==oper[j])
            {
                str[i]='';
                keyw(str);
            }
        }   
        for(j=0;j<=14;j++)
        {
            if(i==-1)
                break;
            if(ch==seps[j])
            {
                if(ch=='#')
                {
                    while(ch!='>')
                    {
                        ch=fgetc(f1);
                    }   
                    i=-1;
                    break;
                }
                if(ch=='"')
                {
                    do
                    {
                        ch=fgetc(f1);
                    }   
                    while(ch!='"');
                    i=-1;
                    break;
                }
                str[i]='';
                keyw(str);
            }
        }
        if(i!=-1)
        {
            str[i]=ch;
            i++;
        }
        else
            i=0;
    }
    return 0;
}
void keyw(char *p)
{
    int k,flag=0;
    for(k=0;k<=31;k++)
    {
        if(strcmp(keys[k],p)==0)
        {
            for(n=0;n<33;n++)
            {
                if(strcmp(s[n],p)==0)
                    continue;
                else
                {
                    strcpy(s,p);
                    cout<<s[n]<<"is a keyword";
                    flag=1;
                    break;
                }
            }
        }
    }
    if(flag==0)
    {
        if(isdigit(p[0]))
            num++;
        else
        {
            if(p[0]!='')
                printf("%s is an identifiern",p);
        }   
    }
    i=-1;
}

//错误:lex.cpp:在函数void keyw(char*)中:Lex.cpp:91:18:错误:从' char '到' const char* '的转换无效[-fpermissive]如果比较字符串(s [n], p) = = 0)^从lex.cpp:4:0包含的文件中:/usr/include/string.h:144:12:错误:初始化' int strcmp(const char*, const char*) '的参数1 [-fpermissive]Extern int STRCMP (const char *__s1, const char *__s2)

这一行不对:

keyw(str);

而不是你需要:

keyw(&str);

函数keyw(char *p)需要一个指针或地址。