#include <stdio.h>
//值传递
void swap(int m,int n)
{
int temp;
temp = m;
m = n;
n = temp;
printf("m=%d,n=%d",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 hun()
{
int value = 666;
return value;
}
//地址返回
int *iun()
{
static int value = 999;//不占函数的内存
return &value;//返回静态局部变量地址
}
int main(int argc, const char *argv[])
{
int num = 520;
int key = 1314;
//调用函数
swap(num,key);
printf("调用后num = %d,key = %d\n",num,key);
//fun
fun(&num,&key);
printf("调用后num = %d,key = %d\n",num,key);
gun(&num,&key);
printf("调用后num = %d,key = %d\n",num,key);
int ret = hun(); //返回值是右值
printf("hun() = %d\n",hun());
int *ptr = iun(); //地址返回结果可以作为右值
*iun()=555; //地址返回结果可以作为左值
printf("*iun()=%d\n",*iun());
printf("*ptr = %d\n",*ptr);
return 0;
}
输出m=1314,n=520调用后num = 520,key = 1314
*p=1314,*q=520
调用后num = 520,key = 1314
*p=1314,*q=520
调用后num = 1314,key = 520
hun() = 666
*iun()=555
*ptr = 555
#include <stdio.h>
#include <stdlib.h>
int m; //未初始化的全局变量,在全局的.bss段
int n = 520; //已初始化的全局变量,在全局区的.data段
static int k; //静态变量,.bss段
static int l = 666;//静态变量,.data段
char arr[100] = "hello world";//arr数组在全局区的.data 段,hello world在.ro段
char *p = "hello";//指针在.data段,hello在.ro段
int main(int argc, const char *argv[])
{
int a;
double b = 999;//局部变量在栈区申请
// printf("&a = %p,&b = %p\n",&a,&b);
static int c;//静态局部变量在全局的.bss段申请
static int d = 520;//静态变量,在.data段申请
char *q = "nihao";//q在栈区申请的8字节,nihao在.ro段
char e[100] = "hello world";
int *ptr = (int *)malloc(sizeof(int));//ptr在栈区,但申请的空间在堆区
return 0;
}
#include <stdio.h>
#include <stdlib.h> //malloc所在头文件
int main(int argc, const char *argv[])
{
int *p1 = (int *)malloc(4);//堆区申请一个int类型的空间大小
printf("*p1 = %d\n",*p1); //随机值
int *p2 = (int *)malloc(sizeof(int)); //申请一个int类型的大小
*p2 = 520;//给堆区空间赋值
printf("*p2 = %d\n",*p2);
int *p3 = (int *)malloc(sizeof(int)*5); //连续申请5个int类型的大小
for(int i=0;i<5;i++)
{
printf("%d\t",*(p3+i));
}
putchar(10);
//释放
free(p1);
p1 = NULL;
free(p2);
p2 = NULL;
free(p3);
p3 = NULL;
return 0;
}输出
*p1 = 0
*p2 = 520
0 0 0 0 0
堆区申请6个int类型空间存放6名学生成绩,输入成绩,输出升序的成绩
#include <stdio.h>
#include <stdlib.h>
int *shenqing(int num)
{
int *p = (int *)malloc(sizeof(int)*num);
if(NULL==p)
{
printf("申请失败\n");
return NULL;
}
else
{
printf("申请成功\n");
return p;
}
}
void input(int *p)
{
int i = 0;
if(NULL!=p){
while(i<6){
printf("第%d位 = ",i+1);
scanf("%d",p+i);
i++;
}
}
}
int xz(int *p) //选择排序
{
int i,j,index,temp;
for(i=0;i<6;i++){
index = i;
for(j=i+1;j<6;j++){
if(*(p+j)<*(p+index)){
index=j;
}
}
temp = *(p+index);
*(p+index) = *(p+i);
*(p+i) = temp;
printf("%d\t",*(p+i));
}}
void myfree(int *p)
{
if(NULL!=p)
{
free(p);
p = NULL;
}
}
int main(int argc, const char *argv[])
{int *p = shenqing(6);
input(p);
printf("输出:\n");
xz(p);
myfree(p);
p = NULL;
return 0;
}输出
申请成功
第1位 = 87
第2位 = 90
第3位 = 78
第4位 = 60
第5位 = 75
第6位 = 84
输出:
60 75 78 84 87 90
#include <stdio.h>
#include <string.h>
typedef unsigned short int uint16; //将无符号short短整形重定义为uint16typedef int * p_i;//int *类型重命名为p_i
typedef char string[10];//char[10]类型重命名为string
int main(int argc, const char *argv[])
{
uint16 num = 520; //等价于unsigned short int num = 520
printf("sizeof num = %ld\n",sizeof(num)); //2(16位)
printf("num = %d\n",num); //520
p_i p1 = NULL; //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;
}输出
sizeof num = 2
num = 520
sizeof p1 = 8
s1=hello
sizeof s1=10
strlen s1=5
#include <stdio.h>
typedef int *ptr_i,int32; //int32是一个int类型的重命名
//ptr_i是int*类型的重命名
int main(int argc, const char *argv[])
{
int32 num; //num是普通变量
ptr_i p; //p是intx类型变量
printf("sizeof num = %ld\n",sizeof(num));
printf("sizeof p = %ld\n",sizeof(p));
return 0;
}输出
sizeof num = 4
sizeof p = 8
#include <stdio.h> //类型重定义与宏定义的区别
#define ptr_i int *
typedef int * pptr_i;
int main(int argc, const char *argv[])
{
ptr_i a, b; //int *a,b
pptr_i m,n; //int *m,int *n
printf("sizeof a = %ld\n",sizeof(a));
printf("sizeof b = %ld\n",sizeof(b));
printf("sizeof m = %ld\n",sizeof(m));
printf("sizeof n = %ld\n",sizeof(n));
return 0;
}输出
sizeof a = 8
sizeof b = 4
sizeof m = 8
sizeof n = 8
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//声明结构体
struct Hero
{
char name[16];
int Hp;
double speed;
int kill;
}h3 = {"盖伦",3500,500,5};
struct Sp//商品
{
char name [32];
char position[32];
float price;
double weight;
};
int main(int argc, const char *argv[])
{
//堆区申请一个英雄类型,完成初始化并输出对应属性
struct Hero *p = (struct Hero*)malloc(sizeof(struct Hero));
//给英雄名赋值
strcpy(p->name,"赵信");
p->Hp = 3000; //给Hp赋值
p->speed = 350;
p->kill = 3;
printf("%s %d %.2f %d\n",p->name,p->Hp,p->speed,p->kill);
struct Hero h1 = {"亚索",650,350,0}; //定义一个英雄变量
struct Hero h2 = {.Hp=2000, .speed=1000};
printf("name = %s\n",h1.name);
printf("Hp = %d\n",h1.Hp);
printf("speed = %.2lf\n",h1.speed);
printf("kill = %d\n",h1.kill);
struct Sp g1={};
printf("名称:");
scanf("%s",g1.name);
printf("产地:");
scanf("%s",g1.position);
printf("价格:");
scanf("%f",&g1.price);
printf("重量:");
scanf("%lf",&g1.weight);
printf("name = %s\n",g1.name);
printf("position = %s\n",g1.position);
printf("price = %.2lf\n",g1.price);
printf("weight = %lf\n",g1.weight);
return 0;
}输出
赵信 3000 350.00 3
name = 亚索
Hp = 650
speed = 350.00
kill = 0
名称:哈密瓜
产地:新疆
价格:20
重量:4
name = 哈密瓜
position = 新疆
price = 20.00
weight = 4.000000