转换无效从char到const char *

时间:2016-11-05 05:52:05

标签: c++ type-conversion

当程序作为文件提供给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[]=" \t\n,;(){}[]#\"<>";
    char oper[]="!%^&*-+=~|.<>/?";
    int j;
    char fname[50];
    FILE *f1;
    printf("enter file path\n");
    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]='\0';
                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]='\0';
                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]!='\0')
                printf("%s is an identifier\n",p);
        }   
    }
    i=-1;
}

//错误:     lex.cpp:在函数'void keyw(char *)'中:     lex.cpp:91:18:错误:从'char'无效转换为'const char *'[-fpermissive]          如果(的strcmp(S [N],P)== 0)                       ^     在lex.cpp:4:0中包含的文件中:     /usr/include/string.h:144:12:错误:初始化'int strcmp(const char *,const char *)'[-fpermissive]的参数1      extern int strcmp(const char * __ s1,const char * __ s2)

1 个答案:

答案 0 :(得分:1)

这一行错了:

keyw(str);

而不是你需要:

keyw(&str);

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