在函数中移动到下一个字符串 [英] move to next string in function

查看:115
本文介绍了在函数中移动到下一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我接受字符串格式用户



  int  main() 
{

char * str;
clrscr();

printf( \ n请输入sql comm ..... \ n );
flushall();
得到(str);
// 用户已输入该字符串select * from std
pch = strtok (str, );
while (pch!= NULL)
{
printf( %s,pch);
moveNext(pch);

}

getch();
return 0 ;
}


void moveNext( char * pch)
{
pch = strtok(NULL, );
}





Quote:

第二次迭代时内部函数pch =*但是当它在main内部调用时它仍然有选择,所以如何在main函数中出现函数更改

解决方案

< blockquote> C中的所有变量都是按值传递的,而不是通过引用传递的 - 因此对函数内部的值所做的任何更改只影响函数内部的副本,而不影响外部世界。

最简单的方法这样做是为了返回值:

  char  * moveNext()
{
return strtok(NULL, );
}



然后:

  while (pch!= NULL)
{
printf( %s< /跨度>,PCH);
pch = moveNext();
}



另一种方法是将pch的地址传递给你的函数而不是值,并改变那些点的内容to - 但返回值更简单,更明显。


I accept string form user

int main()
{
  
	char * str;
	clrscr();
	
	printf("\n please enter sql comm.....\n");
	flushall();
	gets(str);
//user has enterd that string select * from std
	 pch = strtok(str," ");
while(pch != NULL)
	{
         printf("%s",pch);
           moveNext(pch);

        }

	getch();
	return 0;
}


void moveNext(char * pch)
  {			
    pch=strtok(NULL," ");		 
  }



Quote:

Inside function at second iteration pch="*" but when it called inside main it still have select so how to make change in function appear in main function

解决方案

All variables in C are passed by value, not by reference - so any change you make to the value inside the function affects only the copy inside the function, not the outside world.
Teh simplest way to do this is to return the value:

char* moveNext()
    {
    return strtok(NULL," ");
    }


And then:

while(pch != NULL)
    {
    printf("%s",pch);
    pch = moveNext();
    }


The other way to do it is to pass the address of pch through to your function rather than the value, and alter the contents of what that points to - but returning the value is both simpler and a lot more obvious.


这篇关于在函数中移动到下一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆