05.指针

2017-09-15  本文已影响0人  DamageDurex

指针

未初始化和非法指针

// 声明指针a
int *a;

/*a的地址赋值为12,但是a的地址指向哪里呢,声明了指针变量但为初始化,所以没有办法预测
a的值存储在声明地方*/ 

*a = 12;

NULL指针

指针的指针


#include <stdio.h>

int main(void)
{
    int a = 12;
    int *b = &a;
    int **c = &b;

    printf("a=%d\n",a);
    printf("b=%p\n*b=%d\n",b,*b);
    printf("c=%p\n",c);
    printf("*c=%p\n",*c);
    printf("**c=%d\n",**c);
}

上面代码声明了3个变量a,指向a的指针b,指向指针b的指针c,c也叫指针的指针.

表达式 相当的表达式
a 12
b &a
*b a,12
c &b
*c b,&a
**c a,12,*b

指针表达式

char ch = 'a'
char *cp = &ch;
// 各类型表达式
// &ch = cp 变量ch的指针
// cp = &ch 变量ch的指针
// &cp 指针cp的指针
// *cp = ch = 'a' 使用间接访问操作符访问指针cp
// *cp + 1 = 'b' 使用间接访问操作符访问指针cp后加1
// *(cp+1) 指针cp加1后执行间接访问,如果指针cp加1后的地址不存在则是错误的
// ++cp 前缀++先增加它的操作数的值在返回结果,这份拷贝的存储位置并未清晰定义,因此不合法
// cp++ 后缀++先返回cp值的一份拷贝然后再增加cp的值,这样,表达式的值就是cp原来的值的一份拷贝
// *++cp 间接操作符作用于增值后的指针的拷贝上
// ++*cp 首先执行间接访问操作,然后cp所指向的位置的值加1
// (*cp)++ 使用后缀++操作符,必须加上括号,它首先执行间接访问操作
// ++*++cp 首先执行++cp,借这个对这个拷贝值进行间接访问,使得访问ch后的内存位置,然后对这个位置+1
// ++*cp++ 这里后缀++优先级较高,先执行

使用指针计算字符串长度

#include <stdio.h>
int strlen_for(char *);
int strlen_while(char *);
int strlen_goto(char *);
int main()
{
    char *str = "abcde";
    int len;
    len = strlen_for(str);
    printf("for实现:%d\n",len);
    len = strlen_goto(str);
    printf("goto实现:%d\n",len);
    len = strlen_while(str);
    printf("while实现:%d\n",len);
}

int strlen_for(char *string)
{
    int len = 0;
    for(;*string!='\0';string++) 
        len++;
    return len;
}

int strlen_while(char *string)
{
    int len = 0;
    while(*string++!='\0')
        len++;
    return len;
}


int strlen_goto(char *string)
{
    int len = 0;
    LABEL:
        if (*string!='\0') {
            string++;
            len++;
            goto LABEL;
        }
    return len;
}


使用指针进行字符串查找

#include <stdio.h>

int find_char_for(char **strings,int);

int find_char_while(char **strings,int c);

int find_char_goto(char *strings[3],int c);

int main(int argc,const char *argv[])
{
    int result;
    char *strings[4] = {"abc","def","ghi"};
    result = find_char_for(strings,'a');
    printf("for实现:%d\n",result);
    result = find_char_while(strings,'b');
    printf("while实现:%d\n",result);
    result = find_char_goto(strings,'g');
    printf("goto实现:%d\n",result);
    return 0;
}


int find_char_for(char **strings,int c)
{
    // 外层循环:每个字符串数组 数组不为空时
    for ( ; *strings!=NULL; strings++){
        if (*strings == NULL) return 0;
        // 内层循环: 每个字符串数组的字符 字符不等于'\0'时
        for (; **strings!='\0'; *strings++)
            if (**strings == c)
                return 1;
    }
    return 0;
}

int find_char_while(char *strings[3],int c)
{
    while( *strings != NULL){
        if (*strings == NULL) return 0;
        while( **strings != '\0')
            if (*(*strings)++ == c)
                   return 1;
        strings++;    
    }
    return 0;
}


int find_char_goto(char *strings[3],int c)
{

    LABEL1:
       if(*strings != NULL) {
       LABEL2:
          if(**strings!='\0') {
              if(*(*strings)++ == c){
                  return 1;
              }
             goto LABEL2;
          } 
          strings++;
          goto LABEL1;
       }
    return 0;
}

上一篇 下一篇

猜你喜欢

热点阅读