zhliao2

风雨兼程,一路向北-------fpga (Keep a quiet heart study)
返回指针值的函数

int *a ( int x, int y );

a是函数名,调用它以后能得到一个指向整型数据的指针.*a两侧没有括号,在a的两侧分别为*运算符和()运算符

而()的优先级高于*.因此a先与()先结合,这个函数前面有*,表示此函数是指针型函数,最前面的int表示返回的

指针指向整型变量.

 

eg:有三个学生,每个学生有4门课,功能:用户输入学生序号,就能输出该学生的成绩

# include <stdio.h>

int main ( void )
{
    float score [][4] = { { 60, 70, 80, 90 }, { 56, 89, 67, 88 }, { 34, 78,  90, 66 } };
    float *search ( float ( *pointer )[ 4 ], int n );
    float *p;
    int i, m;
    printf ( "enter the number of student:" );
    
    scanf ( "%d", &m );
    printf ( "The score of No.%d are:\n", m );
    p = search ( score, m );      //score的首地址给pointer;
    for ( i = 0; i < 4; i ++ )
    {
        printf ( "%5.2f/t", *( p + i ) );
        printf ( "\n" );
    }
}

float *search ( float ( *pointer )[ 4 ], int n )
{
    float *pt;
    pt = *( pointer + n );
    return ( pt );
}

 结果:

 

posted on 2012-07-15 11:57  zhliao  阅读(270)  评论(0)    收藏  举报