返回指针的函数
#include <stdio.h> int *max(int *x, int *y) { int *t; if(*x > *y) t = x; else t = y; return t; } int main() { int a, b, *p; scanf("%d %d", &a, &b); p = max(&a, &b); printf("max = %d \n", *p); return 0; }
返回值不可以是已经失效的地址(有警告)
#include <stdio.h> int *max(int *x, int *y) { int t; if(*x > *y) t = *x; else t = *y; return &t; } int main() { int a, b, *p; scanf("%d %d", &a, &b); p = max(&a, &b); printf("max = %d \n", *p); return 0; }
时间: 2024-10-02 13:23:48