简体   繁体   English

将字符串作为参数传递

[英]pass string as argument

I am trying to pass string as an argument but result is always pointer.c:13:14: error: cannot convert 'char**' to 'char*' for argument '1' to 'int swg(char*) 我正在尝试将字符串作为参数传递,但结果始终是pointer.c:13:14: error: cannot convert 'char**' to 'char*' for argument '1' to 'int swg(char*)

the string i want to pass in looks like 我想传递的字符串看起来像

char * str

this string gets value using fgets/getline 该字符串使用fgets / getline获取值

my function lookslike this 我的功能看起来像这样

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;
  }

and i am calling it like this 我这样称呼它

int tst;
tst=swg(str)

i even tried using tst=swg(&str) but it didnt work , how can i pass string as argument then? 我什至尝试使用tst=swg(&str)但是它没有用,那我怎样才能将字符串作为参数传递呢?

The line that appears incorrect to me is 在我看来不正确的那行是

tmp=strtok(*g,">");

This should be: 应该是:

tmp=strtok(g,">");

The call tst=swg(str) appears OK. 调用tst=swg(str)看起来tst=swg(str)

To simply fix the conversion mismatch, simply replace tmp=strtok(*g,">"); 要简单地解决转换不匹配问题,只需替换tmp=strtok(*g,">"); by tmp=strtok(g,">"); 通过tmp=strtok(g,">"); .

Anyways, you code appears to have other issues than this. 无论如何,您的代码似乎还有其他问题。

In order to process every token in your input string, you must call strtok() until there are no more tokens to be processed. 为了处理输入字符串中的每个标记,必须调用strtok()直到没有更多的标记要处理。

Find below an example of that. 在下面找到一个例子。

#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;
}

Output: 输出:
Test one: 0 测试一:0
Test two: 1 测试二:1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM