每日练习5.4

函数的参数是指针

用指针输出字符串

int main()
{
	char* p = "graps now,don't... future.";
	printf("%s", p);
	printf("\n");
	return 0;
}

不用strcpy复制字符串

int main()
{
	char str[] = { "abcdefj" };
	char str2[] = { "hijklmn" };
	char* p1, * p2;
	p1 = str;
	p2 = str2;
	while (*p1 != '\0')
	{
		*p2 = *p1;
		p1++;
		p2++;
	}
	*p2 = '\0';

	printf("%s",str2);
	//puts(str2);
	printf("\n");
	return 0;
}

用字符串数组输出

int main()
{
	char *arr[] =
	{
		"chinese",
		"math",
		"english"
	};
	//puts(arr);
	for (int i = 0; i < 3; i++)
	{
		printf("%s", arr[i]);
		printf("\n");
	}
		
	return 0;

使用int**p

int main()
{
	int** p;
	int price = 559;
	int* p1;
	p1 = price;
	p = p1;

	printf("%d\n", p);

	int i = 0;
	char* price[] = { "559" };
	char** p;

	for (i = 0; i < 1; i++)
	{
		p = price + i;
		printf("%s", *p);
	}
	return 0;
}

函数参数为指针

int area(int* l, int* w, int* h)
{
	//int s = 0;
	//s = (*l) * (*w) * (*h);
	return (*l) * (*w) * (*h);
}
void main()
{
	int lenth = 10;
	int wide = 5;
	int hight = 15;
	int* l = &lenth;
	int* w = &wide;
	int* h = &hight;

	printf("%d\n", area(l, w, h));
}

从小到大输出

void swap(int* q1, int* q2)
{
	int temp;
	temp = *q1;
	*q1 = *q2;
	*q2 = temp;
	
}
void exchange(int* x, int* y, int* z)
{
	if (*x < *y)
		swap(x, y);
	if (*x < *z)
		swap(x, z);
	if (*y < *z)
		swap(y, z);
}
int main()
{
	int a, b, c;
	int* p1, * p2, * p3;
	scanf("%d, %d, %d", &a, &b, &c);
	p1 = &a;
	p2 = &b;
	p3 = &c;

	exchange(p1, p2, p3);
	printf("%d, %d, %d\n", a, b, c);
	return 0;
}

练习

void order(int* a, int* b)
{
	if (*a > *b)
	{
		int imup = 0;
		imup = *a;
		*a = *b;
		*b = imup;
	}
}
int main()
{
	int a, b, * p1, * p2;
	scanf("%d %d", &a, &b);

	p1 = &a;
	p2 = &b;
	order(p1, p2);
	printf("%d %d", a, b);
	return 0;
}

求和

int sum(int* q)
{
	int Sum = 0;
	for (int j = 0; j < 5; j++)
	{
		 Sum = *q + Sum;
		 q++;
	}
	return Sum;
}
void main()
{
	int arr[5], * p;
	for (int i = 0; i < 5; i++)
	{
		scanf("%d", &arr[i]);
	}
	p = arr;
	

	printf("%d\n", sum(p) );
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值