04. C Pro 关于指针/指针函数/函数指针


/* 1. 指向同一数组的两个指针的差 == 相隔的元素个数,不是字节数
int main(void) {
  int i, t, *p, *q, a[10] = { 1,3,5,7,9,11,13,15,17,19 };
  for (p = a, i = 0; i < 10; i++) {
    printf("%4d",*(p+i));
  }
  printf("\n");
  q = a + 9;
  while (p < q) {
    t = *p; *p = *q; *q = t;
    p++; q--;
  }
  for (p = a; p - a < 10; p++) {
    printf("%4d", (*p));
  }
  return 0;
}
*/

/* 2. void指针,可以接受任何类型的指针变量,使得一个函数可以处理不同类型的参数

void half(void * pval, char type) { // void * 可以接受任意类型的指针变量
  switch (type)
  {
    case 'i': {
      *((int*) pval) /= 2; //强制转换为 int 指针
      break;
    }
    case 'l': {
      *((long*) pval) /= 2; //强制转换为 long 指针
      break;
    }
    case 'f': {
      *((float*) pval) /= 2; //强制转换为 float 指针
      break;
    }
    case 'd': {
      *((double*) pval) /= 2; //强制转换为 double 指针
      break;
    }
    default:
      break;
  }
}

void main() {
  int i = 10;
  long l = 100000;
  float f = 12.456;
  double d = 123.044444;
  half(&i, 'i');
  half(&l, 'l');
  half(&f, 'f');
  half(&d, 'd');
  printf("%d\n", i);
  printf("%ld\n", l);
  printf("%f\n", f);
  printf("%lf\n", d);

}
*/

/* 3. 字符串指针的移动 == 数组方式

int main(void) {
  char *p;     // char/ string type 指针
  p = "this is a book";   //赋值为string
  printf("%s", p + 10);   //按char类型移动指针
  return 0;
}

*/

/* 4. 指向指针变量的指针:二级指针
void main() {
  char *ps[] = { "follow me", "basic", "great wall", "fortran" };
  char **p;  // 定义指向指针的指针
  for (int i = 0; i < 4; i++) {
    p= ps + i;
    printf("%s\t", *p);
  }
}
*/

/* 5.通过指针的指针,实现内存地址的交换
void sort(char **p) {
  int i, j;
  char *pc;
  for (i = 0; i < 5; i++) {
    for (j = i + 1; j < 5; j++) {
      if (strcmp(*(p + i), *(p + j)) > 0) {  //两个指针,指向的是str[ i/j ][ ],所以这里比较的是两个字符数组 == 字符串
        pc = *(p + i);  *(p + i) = *(p + j);  *(p + j) = pc;
        printf("%d\t%d\t", *(p + i), *(p + j));
      }
    }
  }
}
void main() {
  int i;
  char **p, *ps[5], str[5][10];
  for (i = 0; i < 5; i++)
    ps[i] = str[i];


  printf("input five strings:");
  for (i = 0; i < 5; i++)
    scanf("%s", ps[i]);

  p = ps;
  sort(p);
  for (i = 0; i < 5; i++)
    printf("%s\t", ps[i]);
}
*/

指针函数:函数的返回值是一个地址 -》指针

函数指针:指向函数的指针,可以通过传递指针的方式传递函数

/* 6. 函数指针的调用

float max(float x, float y) { return x>y?x:y; }

float min(float x, float y) { return x

float (*func) (float, float);

void main(){

  float x=1.5, y=2.5;

  float (*func);

  func = max;

  printf("%f\n", func(x, y));

  func = min;

  printf("%f\n", (*func)(x, y));

*/

/* 7. 用函数指针作为函数参数
float plus(float x, float y) { return x + y; }
float minus(float x, float y) { return x - y; }
float multiply(float x, float y) { return x * y; }
float divide(float x, float y) { return x / y; }
float mathfunc(float(*func)(float, float), float x, float y) {
  return (*func)(x, y);
}

void main() {
  float x = 1.5, y = 2.5;
  printf("\nx+y=%f", mathfunc(plus, x, y));
  printf("\nx-y=%f", mathfunc(minus, x, y));
  printf("\nx*y=%f", mathfunc(multiply, x, y));
  printf("\nx/y=%f", mathfunc(divide, x, y));
}
*/

/* 8. 指向结构体的指针
struct stu {
  int num;
  char name[15]; //字符串在结构体中的赋值和引用
  char sex;
  float score;
}boy[5] = {
  {101,"Zhou ping",'M',45},
  {102,"Zhang ping",'M',62.5},
  {103,"Liou fang",'F',122.5},
  {104,"Cheng ling",'F',87},
  {105,"Wang ming",'M',58}
};
void main() {
  struct stu *st;
  printf("No\tName\t\tSex\tScore\t\n");
  for (st = boy; st - 5 < boy;st++) {
    printf("%d\t%s\t%c\t%f\t\n",st->num,st->name,st->sex,st->score);
  }
}
*/

/* 9.计算一个日期是当年的第几天
struct date {
  int year;
  int month;
  int day;
};
int count_days(struct date dt) {
  int i, days = 0;
  int months[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
  for (i = 1; i < dt.month; i++) {
    days += months[i];
  }
  if ((dt.year % 4 == 0 && dt.year % 100 != 0 || dt.year % 400 == 0) && dt.month>2)
    days += 1;
  days += dt.day;
  return days;
}
void main() {
  struct date dt;
  printf("input date:");
  scanf("%d-%d-%d", &dt.year,&dt.month,&dt.day);
  printf("\ncount days %d",count_days(dt));
}
*/

/* 10. 输出一种排列
//e[] & color[] 模拟 enum / set
int e[5], book[5], n=3;
char color[][10]= {"red","yellow","blue","white","black"};

void dfs(int step) {
  if (step == n) {
    for (int i = 0; i < n; i++) {
      printf("%d\t", e[i]);
      printf("%s\t", color[e[i]]);
    }
    printf("\n");
    return;
  }
  for (int i = 0; i < 5; i++) {
    if (book[i] == 0) {
      book[i] = 1;
      e[step] = i;
      dfs(step + 1);
      book[i] = 0;
    }
  }
  return;
}

void main() {
  dfs(0);
}
*/

相关