//
// main.m
// C7_指针
//
// Created by YIem on 15/11/18.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import <Foundation/Foundation.h>

void printArray(int a[], int count);//a[] = *a
void printArray(int *a, int count) {

}

int main(int argc, const char * argv[]) {

if 0


// 指针 pointer  指针是变量
// 指针变量 专门保存 地址数据
// 类型 变量名 = 初值;
int      a    =   0;
int *    p   =  NULL;
// int * 指针变量类型
// p  变量名
// NULL 空指针
int *p1 = NULL;// 通常写法
int * p2 = NULL;//
int*p3 = NULL;
int* p4 = NULL;
int b = 0, c = 0, *p5 = NULL;
// 获取地址
int d = 100;
int *p6 = NULL;
// &取地址符 获取某个变量空间的地址
p6 = &d;
// %p 打印地址
printf("%p\n", p6);
printf("%p\n", main);

float e = 0;
float *pE = NULL;
pE = &e;
printf("%p\n", pE);

endif

if 0

// 直接访问和间接访问
int a = 100;
int *p = NULL;
p = &a;
// 直接访问 a = 200;
a = 200;
printf("%d\n", a);
// 间接访问
// * 间接寻址访问符
*p = 300;
printf("%d\n", a);
// 区分
// int *p ->int *是类型/p是变量名   -> 之后的 = 后 填地址
// *p     ->* 寻址访问符号/p是变量名 -> 之后的 = 后 填值
// a == *P == *(&a) 直接访问 间接访问 *p == *(&a)
*(&a) = 400;
printf("%d\n", a);


//*****************//

int b = 100;
p = &b;
*p = 200;
printf("%d %d\n", a, b);

// 练习: 交换两个变量的值

// int temp = b;
//
// int temp2 = a;
//
// printf("%d %dn", temp, b);


int c = 3, d = 5;
int *pc = NULL, *pd = NULL;
pc = &c;
pd = &d;
int temp = *pc;
*pc = *pd;
*pd = temp;
printf("%d %d\n", c, d);

endif

if 0

// 指针的算术运算
int a = 3, b = 5, c = 7, d =9;
int *p = NULL;
p = &a;
p = &b;
printf("%p\n", p);
// 地址+ 1 向内存 高位移动4个字节
printf("%p\n", p + 1);
// 地址- 1 向内存 低位移动4个字节
printf("%p\n", p - 1);
// 取值
p = &d;
printf("%d\n", *p);
printf("%d\n", *(p + 0));

// printf("%dn", *p + 1);

printf("%d\n", *(p + 1));
printf("%d\n", *(p + 2));
printf("%d\n", *(p + 3));

endif

if 0

// 数组和指针的关系

int a[] = {1, 2, 3, 4, 5};
int *p = NULL;
printf("%p\n", a);
// syntax sugar 语法糖
printf("%d\n", a[0]);
printf("%d\n", *(a + 0));// 等于 *a = *(a + 0)
// 指针变量中保存数组地址
p = a;
printf("%d\n", p[1]);
printf("%d\n", *(p + 1));
// 数组名是常量地址不能被改变 a 本身不能被改变

// a++;// 错

// p是指针变量 可以被改变
p++;// 对

// 普通形式-> 指针
// 数组名[下标] -> *(数组名 + 下标)

// 指针版遍历数组

for (int i = 0; i < 5; i++) {
    printf("%d ", a[i]);
    printf("%d \n", *(a + i));
}

// 指针版冒泡
for (int i = 0; i < 5 - 1; i++) {
    for (int j = 0; j < 5 - i - 1; j++) {
        if (*(a + j) < *(a + j + 1)) {
            int temp = *(a + j);
            *(a + j) = *(a + j + 1);
            *(a + j + 1) = temp;
        }
    }
    
}
for (int i = 0; i < 5; i++) {
    printf("%d \n", *(a + i));
}

printf("------------------------\n");


// 指针类型不匹配
 //小类型 保存大类型
short b[] = {1, 2, 3, 4};
int *p1 = b;

printf("%dn", *(p1 + 0));

// 大类型 保存小类型
int b[] = {1, 2, 3, 4};
short *p1 = b;
printf("%d\n", *(p1 + 0));

//

endif


// 指针和字符串的关系
char str[] = "iPhone";
char *p = str;
printf("%p %p\n", str, p);
// 常量字符串 在常量区
char *str1 = "iPhone";
printf("%p\n", str1);
// 打印字符串
printf("%s %s %s\n", str, p, str1);
printf("%c %c\n", str[1], *(str + 1));
printf("%c %c\n", p[1], *(p + 1));
printf("%c %c\n", str1[1], *(str1 + 1));

printf("%lu %lu %lu\n", strlen(str), strlen(p), strlen(str1));

// 指针数组
char *names[] = {"Marray","Dawa", "Sanye"};
for (int i = 0; i < 3; i++) {
    printf("%s %s\n", names[i], *(names + i));
}

printArray(<#int *a#>, <#int count#>);


return 0;

}