值传递、地址传递、值返回、地址返回
#include
#include
#include
void swap(int m, int n) //值传递
{
int temp;
temp = m;
m = n;
n = temp;
printf("m = %d, n = %d\n", m, n);
}
void fun(int *p, int *q) //值传递
{
int *temp;
temp = p;
p = q;
q = temp;
printf("*p = %d, *q = %d\n", *p, *q);
}
void gun(int *p, int *q) //地址传递
{
int temp;
temp = *p;
*p = *q;
*q = temp;
printf("*p = %d, *q = %d\n", *p, *q);
}
int hui() //值返回
{
int value = 66;
return value;
}
int *iun()
{
static int value = 99; //静态变量虽然在函数体内定义,
//但是不占函数的内存空间
return &value; //返回静态局部变量的地址
}
int main(int argc, const char *argv[])
{
int num = 520;
int key = 1314;
//调用swap函数交换两数
swap(num, key);
printf("调用swap后,主函数中num = %d, key = %d\n", num, key);
//num=520 key=1314
//调用fun函数交换两数
fun(&num, &key);
printf("调用fun后,主函数中num = %d, key = %d\n", num, key);
//num=520 key=1314
//调用gun函数交换两数
gun(&num, &key);
printf("调用gun后,主函数中num = %d, key = %d\n", num, key);
//num=1314 key=520
//调用hui函数
int ret = hui(); //值返回的函数返回值只能是右值
printf("hui() = %d\n", hui()); //66
//调用iun函数
int *ptr = iun(); //地址返回的结果可以作为右值
*iun() = 555; //地址返回的结果可以作为左值
printf("*iun() = %d\n", *iun()); //555
printf("*ptr = %d\n", *ptr); //555
return 0;
}
动态内存分配和回收(malloc、free)
#include
#include
#include
//定义从堆区申请空间的函数,num表示要申请空间的个数
int *memory(int num)
{
//从堆区申请num个int类型的大小空间
int *p = (int *)malloc(sizeof(int)*num);
//判断是否申请成功
if(NULL==p){
printf("内存申请失败\n");
return NULL;
}else{
printf("申请成功\n");
return p;
}
}
//定义输入函数,p表示指向堆区空间的地址,n表示堆区空间元素个数
void input(int *p,int n)
{
if(NULL!=p){
for(int i=0;i*(p+j+1)){
temp=*(p+j);
*(p+j)=*(p+j+1);
*(p+j+1)=temp;
}
}
}
printf("成绩排序成功\n");
}
//定义输出函数,p表示指向堆区空间的地址,n表示堆区空间元素个数
void output(int *p,int n)
{
int i;
for(i=0;i
类型重定义
#include
#include
typedef unsigned short int uint16; //将无符号短整形重命名为uint16
typedef int * Ptr_i; //将int*类型重命名为Ptr_i
typedef char String[10]; //将char [5]类型重命名为String
int main(int argc, const char *argv[])
{
uint16 num = 520; //等价于unsigned short int num = 520
printf("sizeof num = %ld\n", sizeof(num)); //2
printf("num = %d\n", num); //520
Ptr_i p1 = NULL; //此时p1是指针变量 int *p1;
printf("sizeof p1 = %ld\n", sizeof(p1)); //8
String s1; //此时是定义的长度为10的字符数组
strcpy(s1, "hello");
printf("s1 = %s\n", s1); //hello
printf("sizeof s1 = %ld\n", sizeof(s1)); //10
printf("strlen s1 = %ld\n", strlen(s1)); //5
return 0;
}
结构体
#include
#include
#include
//声明一个英雄结构体类型
struct Hero
{
char name[20]; //姓名
int Hp; //血量
double speed; //基础位移
int kill; //人头数
};
struct Goods
{
char name[20];
char position[20];
float price;
float weight;
};
int main(int argc, const char *argv[])
{
struct Goods g1;
scanf("%s",g1.name);
scanf("%s",g1.position);
scanf("%f",&g1.price);
scanf("%f",&g1.weight);
printf("g1.name=%s\n",g1.name);
printf("g1.position=%s\n",g1.position);
printf("g1.price=%f\n",g1.price);
printf("g1.weight=%f\n",g1.weight);
//在堆区申请一个英雄类型,完成初始化并输出相应的属性
struct Hero *ptr = (struct Hero*)malloc(sizeof(struct Hero));
//给英雄名字赋值
strcpy(ptr->name,"亚瑟"); //给姓名赋值
ptr->Hp = 3000; //给hp属性赋值
ptr->speed = 350;
ptr->kill = 3;
//输出英雄指针指向堆区空间中的内容
printf("英雄信息为:%s %d %.2lf %d\n", ptr->name, ptr->Hp, ptr->speed, ptr->kill);
return 0;
}
思维导图