从char到const char*的转换无效
Conversion not valid from char to const char*
我编写了这个程序,用于在程序作为文件提供给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)需要一个指针或地址。
相关文章:
- 将无符号char*转换为std::istream*C++
- 为什么这个函数将"const char*"转换为"void* const"而不是"const void*"
- 将函数参数"const char*"转换为"std::string_view"是
- C ++如何在使用"tolower"时将char转换为int
- 有没有办法在C++的赋值中将"char**"转换为"double"?
- JavaCPP 错误:无法将参数"1"的"char*"转换为"Abc*
- 为什么从 char 转换为 std::byte 可能是未定义的行为?
- 如何在虚幻引擎4中将char*转换为TCHAR?
- C++:将模板类型 char 转换为 std::string
- strcmp/char* 转换无法按预期工作
- C++中的SQLite char*转换
- 无法从 const char* 转换为 const char *&
- 在 C++ 中将 char 转换为 int,而无需符号位传播
- 如果必须包含西里尔字符,如何将 char* 转换为 std::string?
- 试图使用strCMP时的char转换错误
- 无法从'const char *'转换为'LPCTSTR'
- 将 std::vector<char> 转换为字符* 会导致字符缺陷
- 使用 static_cast 将 char 转换为 int c++
- 如何在 C++(MFC) 中将 char* 转换为 LPCTSTR
- 无法将const char转换为字符串构造字符串