分类目录归档:C

C语言关于函数指针的&和*

C89,调用函数指针,需要在函数指针前加*,对函数指针赋值,需要在函数名前面加&

C89,以后,对函数指针操作不需要& 和 *,但,使用它们是个好习惯

如下代码(C89函数指针风格)

#include <stdio.h>
#include <stdlib.h>
int max(int x, int y)
{
    return (x > y ? x :y);
}
int main ( int argc, char *argv[] )
{
    int (*p)(int,int) = &max;	// 这里&max
    int a, b, c;
    scanf("%d%d", &a, &b);
    c = (*p)(a,b);	// 这里(*p)
    printf("%d\n", c);
    return EXIT_SUCCESS;
} 

C语言拆分字符串strtok

Example
/* STRTOK.C: In this program, a loop uses strtok
 * to print all the tokens (separated by commas
 * or blanks) in the string named "string".
 */
#include <string.h>
#include <stdio.h>
char string[] = "A string/tof ,,tokens/nand some  more tokens";
char seps[]   = " ,/t/n";
char *token;
void main( void )
{
   printf( "%s/n/nTokens:/n", string );
   /* Establish string and get the first token: */
   token = strtok( string, seps );
   while( token != NULL )
   {
      /* While there are tokens in "string" */
      printf( " %s/n", token );
      /* Get next token: */
      token = strtok( NULL, seps );
   }
}