#include
/**********值传递***********/
void swap(int m,int n)
{
/*相当于赋值,将a赋值给m*/
/*b赋值给n,m与n的变化不影响a和b*/
int temp;
temp=m;
m=n;
n=temp;
printf("%d %d\n",m,n);
}
/**********地址的值传递*****/
void fun(int *p,int *q)
{
/*本质是指针指向的互换,没有改变指向的内容*/
/*即可理解为地址的赋值同上*/
int *temp;
temp=p;
p=q;
q=temp;
printf("%d %d\n",*p,*q);
}
/**********地址传递*****/
void gun(int *p,int *q)
{
/*这里是通过指针操作地址上的内容,所以改变了*/
int temp;
temp=*p;
*p=*q;
*q=temp;
printf("%d %d\n",*p,*q);
}
/**********值返回***********/
int hun()
{
int value=666;
return value;
}
/*********地址返回***********/
int *iun()
{
/*指针函数返回值为地址*/
//可理解为int *iun=&value
static int value=999;
/*static延长了生命周期*/
return &value;
}
int main(int argc, const char *argv[])
{
int a=520;
int b=1314;
swap(a,b);
printf("%d %d\n",a,b);
fun(&a,&b);
printf("%d %d\n",a,b);
gun(&a,&b);
printf("%d %d\n",a,b);
int ret=hun();
printf("%d\n",ret);
int *ptr=iun();
*iun()=555;
printf("%d %d\n",*ptr,*iun());
/*两个指针指向一个地址,一个更改指向内容,都更改,所以为555 555*/
//即理解为*ptr=&value和*ium()=&value
return 0;
}
练习:要求在堆区申请6个int类型空间存放6名学生的成绩,分别使用函数实现申请空间、输入学生成绩、输出学生成绩、对学生进行升序排序、释放空间
#include
#include
#include
//定义从堆区申请空间的函数,num表示要申请空间的个数
int *memory(int num)
{
//从堆区申请num个int类型的大小空间
int *ptr = (int *)malloc(sizeof(int) * num);
//判断是否申请成功
if(NULL == ptr)
{
printf("内存申请失败\n");
return NULL;
}else
{
printf("申请成功\n");
return ptr;
}
}
//定义输入函数,ptr表示指向堆区空间的地址,num表示堆区空间元素个数
void input(int *ptr, int n)
{
if(NULL != ptr)
{
//开始进行输入工作
for(int i=0; ip[j+1])
{
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
}
}
printf("排序成功\n");
}
void out_(int *p, int n)//输出函数
{
int i;
for(i=0;i
#include
#include
#include
struct Hero//声明一个Hero结构体类型
{
//成员为:name Hp speed kill
char name[20];
int Hp;
double speed;
int kill;
};
struct Sp//声明一个Sp结构体类型
{
//成员为:name position price weight
char name[20];
char position[20];
double price;
double weight;
};
int main(int argc, const char *argv[])
{
struct Sp sp1;//定义结构体变量sp1
printf("请输入:商品名、产地、单价、重量:\n");
//成员运算符.
scanf("%s%s%lf%lf",sp1.name,sp1.position,&sp1.price,&sp1.weight);
printf("%s\n",sp1.name);
printf("%s\n",sp1.position);
printf("%.2lf\n",sp1.price);
printf("%.2lf\n",sp1.weight);
return 0;
}
ubuntu@ubuntu:1$ gcc jgt.c
ubuntu@ubuntu:1$ ./a.out
英雄信息为:亚瑟 3000 350.00 3
商品信息为:百事可乐 Chain 4.00 600.00
ubuntu@ubuntu:1$ cat jgt.c
#include
#include
#include
struct Hero//声明一个Hero结构体类型
{
//成员为:name Hp speed kill
char name[20];
int Hp;
double speed;
int kill;
};
struct Sp//声明一个Sp结构体类型
{
//成员为:name position price weight
char name[20];
char position[20];
double price;
double weight;
};
int main(int argc, const char *argv[])
{
//在堆区申请一个英雄类型,完成初始化并输出相应的属性
struct Hero *ptr = (struct Hero*)malloc(sizeof(struct Hero));
//在堆区申请一个商品类型,完成初始化并输出相应的属性
struct Sp *ptr1 = (struct Sp*)malloc(sizeof(struct Sp));
//结构体指针访问成员使用运算符"->"
strcpy(ptr->name,"亚瑟");
ptr->Hp = 3000;
ptr->speed = 350;
ptr->kill = 3;
strcpy(ptr1->name,"百事可乐");
strcpy(ptr1->position,"Chain");
ptr1->price = 4;
ptr1->weight = 600;
//输出英雄指针指向堆区空间中的内容
printf("英雄信息为:%s\t%d\t%.2lf\t%d\n", ptr->name, ptr->Hp, ptr->speed, ptr->kill);
//输出商品指针指向堆区空间中的内容
printf("商品信息为:%s\t%s\t%.2lf\t%.2lf\n", ptr1->name, ptr1->position, ptr1->price, ptr1->weight);
free(ptr);
ptr=NULL;
free(ptr1);
ptr1=NULL;
return 0;
}