• 指针应用


    //
    //  main.c
    //  指针应用
    //
    //  Created by zhangxueming on 15/6/3.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #include <stdio.h>
    
    //指针与函数
    
    void swap(int *a, int *b)
    {
        int temp = *a;
        *a = *b;
        *b = temp;
    }
    
    //int main(int argc, const char * argv[]) {
    //    int a=10, b=20;
    //    
    //    swap(&a,&b);
    //    
    //    printf("a = %d b = %d
    ", a, b);
    //    return 0;
    //}
    
    //利用指针,输入10个数到数组中, 再输出
    
    //int main(int argc,const char *argv[])
    //{
    //    int a[10];
    //    int *p = a;
    //    for (int i=0; i<10; i++) {
    //        scanf("%d", p++);
    //    }
    //    p=a;
    //    for (int i=0; i<10; i++) {
    //        printf("%d ", *p);
    //        p++;
    //    }
    //    printf("
    ");
    //    
    //    return 0;
    //}
    
    
    //指针实现数组逆序
    //数组作为函数的参数, 丢失一个长度
    void reverseArray(int *a, int len)
    {
        for (int i=0; i<len/2; i++) {
            int temp = *(a+i);
            *(a+i)=*(a+len-1-i);
            *(a+len-1-i)=temp;
        }
    }
    //int main(int argc,const char *argv[])
    //{
    //    int a[10]={1,2,3,4,5,6,7,8,9,10};
    //    reverseArray(a, 10);
    //    for (int i=0; i<10; i++) {
    //        printf("%d ", *(a+i));
    //    }
    //    return 0;
    //}
    
    //int a[10]; --> int [10] --> int *
    
    //int main(int argc,const char **argv)
    //{
    //    int a[10]={};//
    //    int *p = a;
    //    printf("%p
    ", p);
    //    printf("%p
    ", a);
    //    
    //    p++;
    //    printf("%p
    ", p);
    //    printf("%p
    ", a+1);//a int *
    //    
    //    return 0;
    //}
    
    //练习:编写函数 replace, 在一个字符数组里面查找指定字符,并用相应的字符替代。函数原型如下:
    void replace(char *array, char old, char new, int length)
    {
        for (int i=0; i<length; i++) {
            if (*(array+i)==old) {
                *(array+i)=new;
            }
        }
    }
    //int main(int argc,const char *argv[])
    //{
    //    char str[5]={'a','a','b','c','a'};
    //    replace(str, 'a', 'A', 5);
    //    for (int i=0; i<5; i++) {
    //        printf("%c ", *(str+i));
    //    }
    //    return 0;
    //}
    
    
    //练习:编写函数 insert, 向一个字符数组指定位置插入一个字符,后面的字符依次向后移动。函数原型如下:
    //"helloworld"
    void insert(char *array, int index, char new, int length)
    {
        for (int i=length-1; i>=index; i--) {
            *(array+i+1) = *(array+i);
        }
        *(array+index) = new;
    }
    //int main(int argc,const char *argv[])
    //{
    //    char str[12]="helloworld";
    //    insert(str, 5, 'A', 11);
    //    printf("%s
    ", str);
    //    
    //    return 0;
    //}
    
    //const与 指针
    //只读的
    //const type name
    //type const name
    
    int main(int argc,const char *argv[])
    {
        //const int a=100;
        //int const b;
        //a=200;// 变量a是只读变量不能被修改
        
        
    //    int a=10;
    //    int b=20;
    //    const int *p = &a;//*p 是只读的, p可读可写的
    //    //*p =30;
    //    p= &b;
        
    //    int a=10;
    //    const int *const p= &a;//*p p都是只读的
        
    //    int a=10;
    //    const int *p=&a,q=20;// *p q 是只读的 p是可读可写
        
    //    const int *p, *q;// *p *q是只读的, p q是可读可写的
       
     //   const int *const p, *q;//*p p *q 是只读的, q是可读可写的
        
        const int *const p, *const q;// *p *q p q都是只读的
        
        return 0;
    }
  • 相关阅读:
    MySQL查询所有库中表名
    MySQL统计数据库表大小
    Spring Cloud 自定义ConfigServer 解决敏感信息存储问题
    JQuery Ajax执行过程AOP拦截
    虚拟机下的centos断电(非正常关机)后mysql启动不了
    Ubuntu 13.10 如何修改背景色--豆沙绿
    CI框架CodeIgniter伪静态各种服务器设置
    MongoDB中MapReduce不同的写法,不同的结果
    分享个人预算系统源码(含说明文档)
    Java lambda 分组后多列求和
  • 原文地址:https://www.cnblogs.com/0515offer/p/4549982.html
Copyright © 2020-2023  润新知