简体   繁体   English

strpbrk函数 - C.

[英]strpbrk function - C

I am trying to break a string from the occurrence of the given character. 我试图从给定字符的出现中打破一个字符串。 I am using the strpbrk function. 我正在使用strpbrk函数。 But I get this error 但是我得到了这个错误

21 G:\My Programs\C\horse.cpp invalid conversion from `char' to `const char*' 

The code which I used is as follows 我使用的代码如下

char horses[100], h[1];

 char *brokenstring;

 h[0] = 'H';

 brokenstring = strpbrk (horses,h[0]);

I get this error in the line where I use the strpbrk function. 我在使用strpbrk函数的行中出现此错误。 Please help me out. 请帮帮我。

You need to pass a const char * to strpbrk()'s 2nd argument and also you need to terminate the string with null. 您需要将const char *传递给strpbrk()的第二个参数,并且还需要使用null终止字符串。

int main( int argc, char ** argv ) {    

 char horses[100], h[2];
 char *brokenstring;

 h[0] = 'H';
 h[1]=0;   
 brokenstring = strpbrk (horses,h);
 return 0;
}

The second argument to strpbrk must be a string, not a char. strpbrk的第二个参数必须是字符串,而不是char。

It is not clear what you want to do; 目前尚不清楚你想做什么; are you sure you don't need, eg, strtok()? 你确定你不需要,例如,strtok()?

strpbrk() is useful if you have several possible dividing characters (eg H, Y and K -- then you pass "HYK"). 如果你有几个可能的分割字符(例如H,Y和K - 然后你通过“HYK”),strpbrk()很有用。 Otherwise you'll be better served by strchr(). 否则你将更好地服务于strchr()。

You passed the second argument of strpbrk a char, not a pointer to chars. 你传递了strpbrk的第二个参数char,而不是指向chars的指针。

You need to call strpbrk (horses, h) 你需要打电话给strpbrk(马,h)

Looking at http://www.cplusplus.com/reference/clibrary/cstring/strpbrk/ I see that the function looks like this 看看http://www.cplusplus.com/reference/clibrary/cstring/strpbrk/我看到这个函数看起来像这样

char * strpbrk ( const char *, const char * ); 

But you are passing it horses and h[0] . 但是你传递马匹和h[0] h[0] is not a char * , it is actually a char . h[0]不是char * ,它实际上是一个char If you just passed it h you would be OK. 如果你只是通过它h ,你会好起来的。

Edit: as others have said, you actually would need to make sure you null terminated h. 编辑:正如其他人所说,你实际上需要确保你的空终止h。

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

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