两个指针相减的意义,以及指针间的转换问题

一: #include <stdio.h>
 int main()
 {
   int a[4] = {1,5,7,9};                                                                                     
   int* p0 = &a[0];
   int* p3 = &a[3];
   printf("p0: = %p ,p3 = %p, res = %lu\n",p0,p3,p3-p0);
  }
两个指针相减代表中间间隔的元素个数。

二:不能将一个struct 转成int,或者将double转成pointer,

将int直接转成struct会报错。如果报以下的错,很可能是不小心犯了我说的这种错误。

error: conversion to non-scalar type requested,

C标准规定标量只能向标量类型转换,算法类型和指针都是标量类型所以向struct或union的转换是错误的(数组,结构体等属于聚合类型,数组有其特殊性)

猜你喜欢

转载自blog.csdn.net/wWX336815/article/details/84584632