函数的递归调用
#include<stdio.h>
int main()
{
void hanoi(int n,char one,char two,char three);
int m;
printf("input the number of diskes;");
scanf("%d",&m);
printf("The step to move %d dickes:");
hanoi(m,'A','B','c');
}
void hanoi(int n,char one,char two,char three)
{
void move(char x,char y);
if(n==1)
move(one,three);
else
{
hanoi(n-1,one,three,two);
move(one,three);
hanoi(n-1,two,one,three);
}
}
void move(char x,char y)
{
printf("%c-->%c/n",x,y);
}
函数与指针引例
#include<stdio.h>
void output(void);
int main(void)
{
void(*p_output)();
p_output=output;
(*p_output)();
return 0;
}
void output(void)
{
printf("LongMai is ok\n");
}
结构体——n个学生的数据
#include<stdio.h>
struct Student
{int num;
char name[20];
float score;
};
int main()
{struct Student stu[5]={{10101,"Zhang",78},{10103,"Wang",98.5},{10106,"Li",86},{10108,"Ling",73.5},{10110,"Sun",100}};
struct Student temp;
const int n=5;
int i,j,k;
printf("The order is:\n");
for(i=0;i<n-1;i++)
{k=i;
for(j=i+1;j<n;j++)
if(stu[j].score>stu[k].score)
k=j;
temp=stu[k];stu[k]=stu[i];stu[i]=temp;
}
for(i=0;i<n;i++)
printf("%6d %8s %6.2f\n",stu[i].num,stu[i].name,stu[i].score);
printf("\n");
return 0;
}
结构体与指针举例
#include<stdio.h>
#define BUF_LEN_MAX 3
#define DATA_LEN_MAX 3
typedef unsigned char uint8_t;
typedef struct
{
uint8_t head;
uint8_t len;
uint8_t add;
uint8_t data[DATA_LEN_MAX];
uint8_t chk;
}RF_FRAME;
RF_FRAME txbuf [BUF_LEN_MAX]={
{0xAA,6,0x00,0x11,0x11,0x11,0xE3},
{0xAA,6,0x01,0x22,0x22,0x22,0x17},
{0xAA,6,0x02,0x33,0x33,0x33,0x4B}
};
void output(RF_FRAME *p_ptr);
int main(void)
{
RF_FRAME *ptr_tmp;
ptr_tmp=txbuf;
output(ptr_tmp);
return 0;
}
void output(RF_FRAME *p_ptr)
{
uint8_t i,j;
for(i=0;i<BUF_LEN_MAX;i++)
{
printf(" %x",p_ptr[i].head);
printf(" %x",p_ptr[i].len);
printf(" %x",p_ptr[i].add);
for(j=0;j<DATA_LEN_MAX;j++)
{
printf(" %x",p_ptr[i].data[j]);
}
printf(" %x\n",p_ptr[i].chk);
}
}
数组指针变量
#include"stdio.h"
int main()
{
int a[6],i,*pa;//定义整形数组和整形指针变量
pa=a;//将指针pa指向数组a*
for(i=0;i<6;i++)
{*pa=i;//将变量i的值赋给由指针变量pa指向的数组a[i]
pa++;//使指针变量pa指向a[i+1]*
}
pa=a;//重新使指针变量pa指向数组a的首地址
for(i=0;i<6;i++)//通过指针的间接访问和指针的移动输出a数组的各个元素
{printf("a[%d]=%d\n",i,*pa);
pa++;
}
return 0;
}
//该程序还可改写为下列简洁的程序形式
/* #include"stdio.h"
{int a[6],i,*pa=a;
for(i=0;i<6;)
{*pa=i;
printf("a[%d]=%d\n",i++,*pa++);
}
return 0;
}
数组指针的引用
#include<stdio.h>
int main(void)
{
char title[]="LongMai C language pointer";
char *p_title;
printf("title: %s\n",title);
p_title=title;
printf("p_title->title: %s\n",p_title);
printf("p_title=%p\n",p_title);
printf("&title=%p\n",&title);
printf("&title[0]=%p\n",&title[0]);
return 0;
}
数组作为参数的函数
#include <stdio.h>
int main()
{int max(int x,int y);
int a[10],m,n,i;
printf("enter 10 integer numbers:");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("\n");
for(i=1,m=a[0],n=0;i,10;i++)
{
if(max(m,a[i])>m)
{m=max(m,a[i]);
n=i;
}
}
printf("The largest number is %d\nit is the %dth number,\n",m,n+1);
}
int max(int x,int y)
{
return(x>y? x:y);
}
移动数组指针
#include<stdio.h>
int main(void)
{
char str[]="C language pointer";
char *p_str;
p_str=str;
while(*p_str)
{
printf("%c : %p\n",*p_str,p_str);
p_str++;
}
return 0;
}