# include<stdio.h>
#include<iostream>
#include<string.h>
#include<malloc.h>
#include <memory.h>
using namespace std;
#define M 4
#define N 4
#define NAMESIZE 32
struct Students//结构体定义和嵌套
{
int Number;
char name [NAMESIZE];
struct birthday
{
int year;
int month;
int day;
}agement;
int age;
};
union//联合体定义
{
int i;
float j;
char str;
}NUM,*p;
void Pointer_one(void)
{
int a=1000;
int *p;
p=&a;
printf("--------程序一状态--------------------\n");
printf("p=%d\na=%d\n",p,*p);
printf("--------------------------------------\n");
printf("\n");
}
void Pointer_two(void)
{
int i;
int b[10]={1,2,3,4,5,6,7,8,9,10};
int *q;
q=&b[0];
printf("--------程序二状态--------------------\n");
for(i=0;i<10;i++)
{
if(*q!='\0')
{
printf("%d",*q);
q++;
}
}
printf("\n");
printf("--------------------------------------\n");
printf("\n");
}
void Pointer_three(void)
{
int Arry[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14};
int *p;
int i;
p=Arry;//p=&Arry[0],
printf("--------程序三状态--------------------\n");
printf("结果一\n");
for(i=0;i<(sizeof(Arry)/sizeof(*Arry));i++)
{
printf("%d",p[i]);
}
printf("\n");
printf("结果二\n");
for(i=0;i<(sizeof(Arry)/sizeof(*Arry));i++)
{
printf("%d",*(p+i));
}
printf("\n");
printf("--------------------------------------\n");
printf("\n");
}
void Pointer_printArry(int *p,int n)
{
int i;
printf("--------程序四状态--------------------\n");
for(i=0;i<n;i++)
{
printf("%d",*(p+i));
}
printf("\n");
printf("--------------------------------------\n");
printf("\n");
}
void Pointer_printArryTwo(int p[1000],int n)//形参*p等价p[N],N为任意数值
{
int i;
printf("--------程序五状态--------------------\n");
for(i=0;i<n;i++)
{
printf("%d",*(p+i));
}
printf("\n");
printf("--------------------------------------\n");
printf("\n");
}
void DoubleArry(void)
{
int i;
int Arry[M][N]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
int *p=*(Arry+0);//&Arry[0][0]/Arry[0]/*Arry指向二维数组的指针必须为列指针
printf("--------程序六状态--------------------\n");
for(i=0;i<M*N;i++)
{
printf("%d ",*(p+i));
}
printf("\n");
printf("--------------------------------------\n");
printf("\n");
}
void DoubleArryTwo(int p[2][4],int m,int n)//p[2][4]行随便定义列不能随便定义
{
int i,j;
printf("--------程序七状态--------------------\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d ",p[i][j]);
printf("\n");
}
printf("\n");
printf("--------------------------------------\n");
printf("\n");
}
void sTR_putout(void)
{
char str[]="Hello Word,Embedded By DriveWheel";
printf("--------程序八状态--------------------\n");
printf("%s\n",str);
puts(str);
printf("字符元素串个数= %d\n字符串类型长度= %d\n",strlen(str),sizeof(str));
printf("\n");
printf("--------------------------------------\n");
printf("\n");
}
void sTR_COPY(void)//串常量不能直接赋值
{
char str[]="Hello World,Embedded By DriveWheel";
printf("--------程序九状态--------------------\n");
printf("用SHEN ZHEN DRIVEWHEEL;CO;LTD替换str\n");
printf("替换前:\n");
printf("%s\n",str);
puts(str);
printf("原元素个数= %d\n原类型长度= %d\n",strlen(str),sizeof(str));
strcpy(str,"SHEN ZHEN DRIVEWHEEL;CO;LTD");
printf("替换后:\n");
printf("%s\n",str);
puts(str);
printf("现元素个数= %d\n现类型长度= %d\n",strlen(str),sizeof(str));
printf("元素被替换但是长度并没有变化\n");
printf("\n");
printf("--------------------------------------\n");
printf("\n");
}
void str_Point_control(void)//指针串常量可以直接赋值
{
char *str="SHENZHEN μπTECHNOLOGY;CO.LTD";
printf("--------程序十状态--------------------\n");
puts(str);
printf("原元素个数= %d\n原类型长度= %d\n",strlen(str),sizeof(str));
str="HelloWorld";
puts(str);
printf("现元素个数= %d\n现类型长度= %d\n",strlen(str),sizeof(str));
printf("\n");
printf("--------------------------------------\n");
printf("\n");
}
void structType_Control(void)//正常操作结构体
{
struct Students person ={ 668899,"μπ",{2014,8,9},25};
printf("--------程序11状态--------------------\n");
printf("结构体程序演示\n");
printf("编号:%d\n姓名:%s\n生日:%d-%d-%d\n年龄:%d\n",person.Number,\
person.name,person.agement.year,person.agem\
ent.month,person.agement.day,person.age);
printf("传参开销:%d\n",sizeof(person));
printf("\n");
printf("--------------------------------------\n");
printf("\n");
}
void structType_control_by_Point(struct Students *p)
{
printf("--------程序12状态--------------------\n");
printf("结构体指针操作程序演示\n");
printf("编号:%d\n姓名:%s\n生日:%d-%d-%d\n年龄:%d\n",p->Number ,p->name\
,p->agement.year ,p->agement.month,p->agement.day,p->age );
printf("传参开销:%d\n",sizeof(p));
printf("\n");
printf("--------------------------------------\n");
printf("\n");
}
void union_control_demo(void)
{
printf("--------程序13状态--------------------\n");
printf("联合体指针操作程序演示\n");
NUM.i =100;
p=&NUM;
printf("%d\ni=%d\n",sizeof(p),p->i);
printf("\n");
printf("--------------------------------------\n");
printf("\n");
}
char *Mine_strcpyDesplay(char *des,const char *loc)//指针函数实现字符串的赋值
{
char *p=des;
printf("--------程序14状态--------------------\n");
printf("指针函数操作程序演示:字符串复制\n");
puts(loc);
puts(des);
printf("复制前目的字符串长度=%d\n复制前被复制字符串\
的长度=%d\n",strlen(des),strlen(loc));
if(des!= NULL && loc!= NULL)
{
while((*des++=*loc++)!='\0')
;
}
return p;
}
int SUM_control(int a,int b)
{
int sum;
sum=a+b;
return sum;
}
int DEC_control(int a,int b)
{
int dec;
dec=a-b;
return dec;
}
void Math_point_con(int a,int b)
{
int (*p[2])(int,int);//函数指针
int t;
int fun;
printf("--------程序15状态--------------------\n");
printf("函数指针操作程序演示\n");
printf("被操作数:%d\n操作数:%d\n",a,b);
p[0]= SUM_control;
p[1]= DEC_control;
for(t=0;t<2;t++)
{
fun=p[t](a,b);
if(t==0)
{
printf("两个数的和为:%d\n",fun);
}
else
printf("两个数的差为:%d\n",fun);
}
printf("\n");
printf("--------------------------------------\n");
printf("\n");
}
void C_TypeCounter(void)
{
printf("--------程序15状态--------------------\n");
printf("函数指针操作程序演示\n");
printf("unsigned int=%d\nsigned int=%d\nunsigned long=%d\nsigned long=%d\n\
float=%d\ndouble=%d\n\
short=%d\nunsigned char=%d\nsigned char=%d\n",sizeof(unsigned int),sizeof(signed int)\
,sizeof(unsigned long),sizeof(signed long),sizeof(float),sizeof(double),\
sizeof(short),sizeof(unsigned char),sizeof(signed char));
printf("\n");
printf("--------------------------------------\n");
printf("\n");
}
void Malloc_memory_allocate(void)//内存分配将数值填进对应的内存空间
{
printf("--------程序16状态--------------------\n");
printf("内存分配函数malloc操作程序演示\n");
int i;
int *p=(int *)malloc(4);
printf("释放前的将结果\n");
for(i=0;i<4;i++)
{
*(p+i)=(i+1)*25;
printf("%d= ",(p+i));
printf("%d\n",*(p+i));
}
printf("p的字节大小=%d\nmalloc的字节大小=%d\n总共分配的字节大小=%d\n",\
sizeof(p),sizeof((int*)(malloc(4))),sizeof(p)+sizeof((int*)(malloc(4))));
printf("\n");
printf("--------------------------------------\n");
printf("\n");
}
void free_malloc()
{
printf("--------程序17状态--------------------\n");
printf("内存分配函数malloc free操作程序演示\n");
typedef struct data_type
{
int age;
char name[20];
int ID;
struct BIR
{
int year;
int month;
int day;
}birth;
}data;
data *bob;
bob = (data *) malloc( sizeof(data) );
printf("指针的大小:%d\n",sizeof(bob));
printf("结构体字节:%d\n",sizeof(data));
//printf("释放前指针所指位置:%d\n",bob);
if( bob != NULL )
{
bob->age = 22;
b