指针

指针是一个变量,其值为另一个变量的地址,即,内存位置的直接地址。就像其他变量或常量一样,您必须在使用指针存储其他变量地址之前,对其进行声明。

指针赋值

//test1.c
#include <stdio.h> int main(void) { int var = 20; int *ip; ip = &var; printf("var addr: %p ", &var ); /*print variable address*/ printf("stored in ip addr: %p ", ip ); /* print pointer to variable address */ printf("value of *ip: %d ", *ip ); /* print pointer to value */ return 0; }

编译后输出:

var asdr: 0x7ffddf977e0c
stored in ip addr: 0x7ffddf977e0c
value of *ip: 20

指针传参

//test2.c
#include <stdio.h> double getAverage(int *arr, int size); /* function declaration*/ int main () { int balance[5] = {1000, 2, 3, 17, 50}; double avg; avg = getAverage( balance, 5 ) ; printf("Average value is: %f ", avg ); return 0; } double getAverage(int *arr, int size) { int i, sum = 0; double avg; for (i = 0; i < size; i++) { sum += arr[i]; } avg = (double)sum / size; return avg; }

编译后输出:

Average value is: 214.400000 

手动计算: (1000+2+3+17+50) / 5 = 214.4

函数指针
函数指针是指向函数的指针变量本质是一个指针变量
通常我们说的指针变量是指向一个整型、字符型或数组等变量,而函数指针是指向函数。
函数指针可以像一般函数一样,用于调用函数、传递参数。

 1 #include <stdio.h>
 2 int max(int x, int y)
 3 {
 4     return x > y ? x : y;
 5 }
 6 
 7 int main(void)
 8 {
 9     int (*p)(int, int) = &max;  /* p is func pointer, &max's & can ellipsis*/
10     int a, b, c, d, max_num;
11     printf("Please enter three numbers:");
12     scanf("%d %d %d", &a, &b, &c);
13     d = p(p(a, b), c);          /* equal max(),d = max(max(a, b), c) */
14     printf("use p(), get the maximum: %d
", d);
15     max_num = max(max(a, b), c);
16     printf("use max(), get the maximum: %d
", max_num);
17     return 0;
18 }

编译后输出:

请输入三个数字:34 77 12
调用p得到最大的数字是:77
调用max得到最大的数字是:77

指针函数

指针函数是指带指针的函数,即本质是一个函数,函数返回类型是某一类型的指针

首先它是一个函数,只不过这个函数的返回值是一个地址值。函数返回值必须用同类型的指针变量来接受,也就是说,指针函数一定有函数返回值,而且,在主调函数中,函数返回值必须赋给同类型的指针变量。 int *func(x, y);

float *fun();
float *p;
p = fun(a);

举个栗子:

 1 #include <stdio.h>
 2 int *GetDate(int wk,int dy);
 3 int main(void)
 4 {
 5     int wk, dy, n=0;
 6     while (n<2) {
 7         do{
 8             printf("Enter week(1-5)day(1-7)
");
 9             scanf("%d %d", &wk, &dy);
10         }
11         while(wk<1||wk>5||dy<1||dy>7);
12         printf("%p
", GetDate(wk, dy));
13         printf("%d
", *GetDate(wk, dy));
14         n++;
15     }
16 }
17 
18 int *GetDate(int wk, int dy)
19 {
20     static int calendar[5][7]={
21         {1, 2, 3, 4, 5, 6, 7},
22         {8, 9, 10, 11, 12, 13, 14},
23         {15, 16, 17, 18, 19, 20, 21},
24         {22, 23, 24, 25, 26, 27, 28},
25         {29, 30, 31, -1},
26     };
27     return &calendar[wk-1][dy-1];
28 }

编译后输出:

Enter week(1-5)day(1-7)
3 4
0x6010a4
18
Enter week(1-5)day(1-7)
3 5
0x6010a8   //0x6010a8比0x6010a4大四字节,若上面calendar定义成char,同时GetDate返回char,则这里会是0x6010a5
19

回调函数

#include <stdio.h>
#include <stdlib.h>
void populate_array(int *array, size_t arraySize, int (*getNextValue)(void)) //int (*getNextValue)(int)
{
    for (size_t i = 0; i < arraySize; i++) 
        array[i] = getNextValue(); //getNextValue(i)
}

int getNextRandomValue(void) //int getNextRandomValue(int val){return val+10;}
{
    return rand();
}

int main(void)
{
    int myarray[5] = {0};
    populate_array(myarray, 5, getNextRandomValue);
    for (int i = 0; i < 5; i++)
    {
        printf("%d ", myarray[i]); //10 11 12 13 14
    }
    printf("
");
    return 0;
}

编译后输出:

1804289383 846930886 1681692777 1714636915 1957747793

原文地址:https://www.cnblogs.com/debruyne/p/9198421.html