//
// main.m
// C10- 动态内存分配
//
// Created by dllo on 15/10/19.
// Copyright (c) 2015年 dllo. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "arrayoperation.h"
int i = 0;
void test(void)
{
for(i = 0; i < 6 ;i++) {
printf("$");
}
}
//char *test1(void)
//{
// char str[] = "qingchun";
// return str; //报错的原因是局部变量在函数结束的时候释放空间
//}
int main(int argc, const char * argv[]) {
// student a[5] = {
// {"yuhais", 'm', 23, 97},
// {"yuhais", 'm', 23, 98},
// {"yuhdais", 'm', 23, 93},
// {"uhais", 'm', 23, 99},
// {"yhais", 'm', 43, 95}
// };
// printstudent(a, 5);
// sortstudent(a, 5);
// printstudent(a, 5);
// for (i = 0; i < 5; i++) {
// if (i >= 3) {
// test();
// }
// printf("*");
// }
//
// int a = 0;
// printf("栈区:%p ",&a);
//
// int *p = malloc(sizeof(int));
// printf("堆区:%p ", p);
//
// static int b = 0;
// printf("静态区%p ",&b);
//
// char *q = "qingchun";
// printf("常量区:%p ",q);
//
// printf("%c ", q[1]);
// printf("%s ", &q[1]);
// char *q = "qingchun";
// q[1] = '0';
//报错 因为qingchun在常量区,常量区只可以读不可以改
// char str[] = "qingchun";
// char *m = str;
// m[1] = '0';
//先将iqngchun复制给数组,在栈区,指针在栈区,可以操作没有报错
// printf("代码区:%p ", test);
//堆内存分配函数
// void *ret = malloc(开辟空间的大小(单位字节))
// 功能: 在堆区开辟指定大小的空间
// 返回值: 返回开辟的空间的首地址
// 参数 : 指定开辟空间的大小, 单位字节
// 在栈区开辟一个int类型的空间
//
// int a = 0;
// 在堆区开辟一个int类型的空间
//int *p = malloc(sizeof(4));
// int *p1 = malloc(sizeof(int));//并不关心什么类型,只关心有多大.不同类型指针接受的意义不同
// *p1 = 3;
// free(p1);
//注意!!!堆内存的空间不会自动的释放,需要手动释放,手动释放函数为free()
// 有一个malloc()就要配对一个free函数
// free(需要释放空间的首地址)
//守护程序,一直都运行
//int arr[5]
// int *p = malloc(sizeof(int) * 5);
// p[1] = 3;
// *(p + 2) = 4;
//int arr[5] 另一种方式(拓展)
// int *p[5] = {NULL};
// for (int i = 0; i < 5; i++) {
// p[i] = malloc(sizeof(int));
// }
//注意
// *(p[1]) = 3;
//返回的空间当做什么类型变量用,取决于用什么类型指针取指向
//int a
// int *p2 = malloc(sizeof(int));
//char c[4]
// char *p3 = malloc(sizeof(int));
//
//有一个字符串,其中包含数字,提取其中的数字,要求动态分配保存
//提示:先计算出有几个谁,然后根据数字的个数来开辟空间
// char str[] = "qing2Ch23un3";
//先记录字符串有多少数字
// char *p4 = str;
// int count = 0;
// for (int i = 0; i < strlen(str); i++) {
// if (p4[i] >= '0' && p4[i] <= '9') {
// count++;
// }
// }
//
// printf("%d ", count);
//再去按照记录结果动态开辟空间
//char *p5 = malloc(sizeof(char) * (count + 1));
//注意,需要为结束符