C 将字符串作为参数传递
我试图将字符串作为参数传递,但结果总是C 将字符串作为参数传递,c,string,C,String,我试图将字符串作为参数传递,但结果总是指针。c:13:14:错误:无法将参数“1”的“char**”转换为“char*”,而将参数“1”转换为“int swg(char*)” 我要传递的字符串看起来像 char * str 此字符串使用fgets/getline获取值 我的函数看起来像这样 int swg ( char *g){ char *tmp; size_t i; tmp=strtok(*g,">"); for ( i = 0 ; i < strlen
指针。c:13:14:错误:无法将参数“1”的“char**”转换为“char*”,而将参数“1”转换为“int swg(char*)”
我要传递的字符串看起来像
char * str
此字符串使用fgets/getline获取值
我的函数看起来像这样
int swg ( char *g){
char *tmp;
size_t i;
tmp=strtok(*g,">");
for ( i = 0 ; i < strlen ( tmp ) ; i ++ ) {
if(tmp[i]=='A') return 0;
}
return 1;
}
我甚至尝试过使用
tst=swg(&str)
但它不起作用,那么我怎么能将字符串作为参数传递呢?我觉得不正确的那行是
tmp=strtok(*g,">");
这应该是:
tmp=strtok(g,">");
调用
tst=swg(str)
看起来正常。要简单地修复转换不匹配,只需替换tmp=strtok(*g,“>”代码>通过tmp=strtok(g,“>”)代码>
无论如何,您的代码似乎还有其他问题
为了处理输入字符串中的每个令牌,必须调用strtok()
,直到没有更多的令牌要处理为止
下面是一个例子
#include <stdio.h>
#include <string.h>
int swg(char g[])
{
size_t i;
char *tmp = strtok(g, ">");
while (tmp != NULL)
{
for (i=0; i<strlen(tmp); i++)
if (tmp[i] == 'A')
return 0;
tmp = strtok(NULL, ">");
}
return 1;
}
int main()
{
char str_one[] = "LOREM>A>IPSUM";
char str_two[] = "LOREM>B>IPSUM";
printf("Test one: %d\n", swg(str_one));
printf("Test two: %d\n", swg(str_two));
return 0;
}
#包括
#包括
int swg(字符g[]
{
尺寸i;
char*tmp=strtok(g,“>”);
while(tmp!=NULL)
{
对于(i=0;iYou正在向函数swg传递一个指向字符串的指针。请尝试tst=swg(*str);
什么不起作用?
#include <stdio.h>
#include <string.h>
int swg(char g[])
{
size_t i;
char *tmp = strtok(g, ">");
while (tmp != NULL)
{
for (i=0; i<strlen(tmp); i++)
if (tmp[i] == 'A')
return 0;
tmp = strtok(NULL, ">");
}
return 1;
}
int main()
{
char str_one[] = "LOREM>A>IPSUM";
char str_two[] = "LOREM>B>IPSUM";
printf("Test one: %d\n", swg(str_one));
printf("Test two: %d\n", swg(str_two));
return 0;
}