//
// main.m
// LessionPointer
//
// Created by laouhn on 15/7/24.
// Copyright (c) 2015年 池海涛. All rights reserved.
//
#import <Foundation/Foundation.h>
void jiaohuan(int *a,int *b)
{
*a = *a^*b;
*b = *a^*b;
*a = *a^*b;
}
int main(int argc, const char * argv[]) {
//指针变量 :存储地址的变量
/**
* 指针定义时,数据类型的作用,告诉系统我是一个什么类型的指针
//指针变量定义时,数据类型的作用(以 int 为例)
1.告诉系统我是一个 int 类型的指针
2.在指针运算是,每次偏移的字节数 int -4 个字节
3.指针控制的字节数,int 控制 4 个字节
*/
/**
int a = 10;
int *p = NULL;//指针变量p
char *q = NULL; //指针q 的数据类型是: char *;
//平时我们所说的指针,其实就是指针变量,指针中存储的是地址
//指针变量中地址的输出
printf("%p\n", p);
//指针变量所占字节数:只与系统操作系统位数有关,32操作系统 4个字节,64位操作系统8个字节
printf("p = %lu\n", sizeof(p));
printf("q = %lu\n", sizeof(q));
//取址运算符: &
p = &a;
printf("%p\n",p);
printf("%p\n", &a);
//取值运算符 *
//printf("%d\n",*p);
//指针变量的赋值:指针重指向,就是对指针变量重新复制的过程.
//指针运算:只有加减运算.
printf("%d\n",*(p+1));
printf("%p\n",p+1);
printf("q+1之后输出的结果%d\n",*(q + 1));
*/
// int a = 10;
// int *p = &a;
// *p = 30;
// //*p 在等号左侧是,是对p中存储的地址进行赋值操作,否则是取值操作
// //int *p = NULL;在定义时*p的作用 高数系统定义的是指针变量.
// printf("%d\n", a);
//指针与数组
/**
*
int a[] = {9, 5, 2, 7};
//数组名是数组首地址,但是数组名是一个常量地址
printf("%p\n",a);
printf("%p\n",&a[0]);
int *p = a;
printf("%d\n",*p);
//指针偏移给指针类型有关
printf("%d\n", *(p + 1));
printf("%d\n",p[1]);
printf("%d\n", *(p + 1 + 1));
printf("%d\n", *(p + 1 + 1 + 1));
printf("%d\n",*a);
printf("%d\n", *(a+1));
printf("%-----d\n",a[1]);
int b[] = {3, 8, 4, 9};
p = b;//指针重指向
//a = b;a是一个常量,不能被赋值,而指针式一个变量,可以重指向(重新赋值).
//数组类型,与指针类型不匹配,会出现什么问题?
int b[] = {256, 5, 2, 7};
int *p = b;
for (int i = 0; i < 4; i++) {
printf("%d", *(p + i));
}
//printf("%d\n",*(q + 1));
char str[] = "iPhone";
char *p = str;
//通过指针输出字符串
printf("%s\n",p);
//把h 改成 H,直接通过地址对里面的值进行修改
是取值运算符
*(p + 2) -= 32;
printf("%s\n", p);
//此种情况,q中存储的是,常量区的地址,而常量区的内容,不允许被修改.切记!!(alt shift +k)
char *q = "iPhone";
(q + 2) = 'H';
printf("q 的地址 %p\n",q);
//通过指针计算字符串的长度.
char str[] = "iPhone";
int i =0;
char *p = str;
while (*(p + i) != 0 ) {
i++;
}
printf("%d",i);
*/
//字符串数组
//char strArray[][20] = {"iPhone", "iPod", "iPad", "iWatch"};
// printf("%s\n",strArray[0]);
//指针数组,里面存得是地址
// char *str[] = {"iPhone", "iPod", "iPad", "iWatch"};
//
// printf("++++%lu",sizeof(str));
// for (int i = 0; i < 4 ; i++) {
// //printf("%p ",str[i]);
// printf("%s ",str[i]);
// }
// printf("\n");
//printf("%p\n",&str[0]);
//char p[] = "ddddd";
//将上述指针数组,升序排序
//str[0] 存储的是iPhone的首地址
//str[1] 存的是iPod 的首地址
//str[2] 存的是ipad 的首地址
//str[3] 存的是iWatch的首地址
/**
*
for (int i = 0; i < 4 -1; i++) {
for (int j = 0; j < 4 - 1 -i; j++) {
if ( strcmp(str[j],str[j+1])>0) {
// *(p + 2) p[2]
//比较的是 内容,交换的是数组中的地址的位置
char *temp =str[j];
str[j] = str[j + 1];
str[j + 1] = temp;
}
}
}
for (int i = 0; i < 4; i++) {
//printf("%p ",str[i]); //输出数组中的元素
printf("%s ",str[i]);//输出数组元素,对应地址上的内容
}
//字符串数组
char str[4][20] = {"iPhone", "iPod", "iPad", "iWatch"};
//指针数组,该指针数组存储的是栈区空间地址
char *strArray[4] = {str[0], str[1], str[2], str[3]};
for (int i = 0; i < 4;i++) {
printf("%p ", strArray[i]);
}
printf("\n");
//指针数组,该指针数组存储的是常量空间的地址
char *str1[4] = {"iPhone", "iPod", "iPad", "iWatch"};
for (int i = 0; i < 4;i++) {
printf("%p ", str1[i]);
}
*/
//指针与函数
int a = 5;
int b = 6;
jiaohuan(&a,&b);
printf("a=%d,b=%d",a,b);
return 0;
}