the application of pointer

来源:互联网 发布:php 文章发布系统源码 编辑:程序博客网 时间:2024/04/29 18:10

Today , the pointer waste me amounts of time;

Last , I find the errror is where  when I define the located

pointer, but I can't assignment the first value, so , when I

call it any time, the vlaule may be different  , so when we

definite the pointer, we must assign the first value to it,

even is NULL, and secondly, I found a secrete of

pointer,

example,

       unsigned char *p = NULL;

      指针p的值是定死的,指针里存储的类型是 unsigned char 型

     unsigned int *p = NULL;

     指针p的值是定死的,指针里存储的类型是unsigned int型,

    所以针对指针进行访问,指针会根据他的类型传递几个字节给你。

    强制类型指针的转换

     addr = 0xfe00;

     unsigned char number1;

     unsigned int number;

     number1 = *(unsigned char *)(addr);

     这里指针会传递一个字符型的

     number2 = *(unsigned int *)(addr);

     这里指针会传递一个int型的

关于sizeof的使用(注意sizeof 记到\0, 结果应该加上\0)

1、数组上

     char abc[100] = {0};

     sizeof(abc) =>> 100;

     sizeof(*abc) >> 1,//一个成员占用了1个字节

     int bcd[100] = {0};

     sizeof(bcd) >> 400;

     sizeof(*bcd) >> 4;


    char abc[] = "123";

    sizeof(abc) >> 4 ;结果加上了\0

    sizeof(*abc) >> 1;

2、指针

     char *p = "abc";

    sizeof (p) >> 4;  //一个指针占用了四个字节空间

    sizeof(*p) >> 1//一个成员占用了1个字节

    int *p = "234";

    sizeof(p) >> 4;//一个指针占用四个字节空间

    sizeof(*p) >> 4;//一个成员占用四个字节空间

3、strlen的使用

     strlen的使用不分数组和字符串,传递的参数是字符型指针,判断长度时候,只要遇到\0就停止,不包括\0;

    char *p = "abcded";

    strlen(p) >>6   //占用六个字节

    char p[100] = {a, b};

   strlen(p) >> 2;//占用两个字节

   int p[100] = {123};

   strlen ((char *)p) >> 1//占用一个字节

   int p[100] = {65535];

   strlen ((char *)p) >> 2//占用两个字节

   int p[100] = {65536];

   strlen((char *)p) >> 3//占用三个字节

        



原创粉丝点击