C程序设计(第五版 谭浩强著)再学习第八章

例题8-1
在这里插入图片描述

#include 

int a = 1, b = 2;
int *pointer1=&a, *pointer2=&b;
int main(void)
{
	printf("a=%d,b=%d\n", a, b);
	printf("*pointer1=%d,*pointer2=%d", *pointer1, *pointer2);
}

C程序设计(第五版 谭浩强著)再学习第八章_第1张图片
C程序设计(第五版 谭浩强著)再学习第八章_第2张图片

#include 

int a = 1, b = 2;
int *pointer1=&a, *pointer2=&b;
int main(void)
{
	printf("a=%d,b=%d\n", a, b);
	printf("*pointer1=%d,*pointer2=%d\n", *pointer1, *pointer2);
	printf("pointer1=%p,pointer2=%p", pointer1, pointer2);
}

C程序设计(第五版 谭浩强著)再学习第八章_第3张图片
例题8-2

在这里插入图片描述
方法1

#include 

int a = 0, b = 0;
int *pointer1= &a, *pointer2= &b;
int bottle = 0;
int main(void)
{
	printf("please type a value and b value:");
	scanf("%d%d", &a, &b);
	if (*pointer1 < *pointer2)
	{
		bottle = *pointer1;
		*pointer1 = *pointer2;
		*pointer2 = bottle;
	}
	printf("%d,%d", *pointer1, *pointer2);
	return 0;
}

C程序设计(第五版 谭浩强著)再学习第八章_第4张图片方法2

#include 

int a = 0, b = 0;
int* pointer1 = &a, * pointer2 = &b, * pointer;
int main(void)
{
	printf("please type a value and b value:");
	scanf("%d%d", &a, &b);
	if (a< b)
	{
		pointer = pointer1;
		pointer1 = pointer2;
		pointer2 = pointer;
	}
	printf("max= %d,min = %d", *pointer1, *pointer2);
	return 0;
}

C程序设计(第五版 谭浩强著)再学习第八章_第5张图片
例题8-3
在这里插入图片描述

#include 

int a = 0, b = 0;
int* pointer1 = &a, * pointer2 = &b;
void swap(int* pointer1, int* pointer2);
int main(void)
{
	printf("please type a value and b value:");
	scanf("%d%d", &a, &b);
	swap(pointer1, pointer2);
	printf("max= %d,min = %d", *pointer1, *pointer2);
	return 0;
}
void swap(int* pointer1, int* pointer2)
{
	int bottle;
	if (a < b)
	{
		bottle = *pointer1;
		*pointer1 = *pointer2;
		*pointer2 = bottle;
	}
}

C程序设计(第五版 谭浩强著)再学习第八章_第6张图片
例题8-4
在这里插入图片描述

#include 

int a = 0, b = 0;
int* pointer1 = &a, * pointer2 = &b;
void swap(int* pointer1, int* pointer2);
int main(void)
{
	printf("please type a value and b value:");
	scanf("%d%d", &a, &b);
	swap(pointer1, pointer2);
	printf("max= %d,min = %d", *pointer1, *pointer2);
	return 0;
}
void swap(int* pointer1, int* pointer2)
{
	int* pointer;
	if (a < b)
	{
		pointer = pointer1;
		pointer1 = pointer2;
		pointer2 = pointer;
	}
}

C程序设计(第五版 谭浩强著)再学习第八章_第7张图片
显然不符合要求。
C程序设计(第五版 谭浩强著)再学习第八章_第8张图片
例题8-5
在这里插入图片描述

#include 
int a = 0, b = 0, c = 0;
int *poninter1 = &a, *poninter2 = &b, *poninter3 = &c;
void exchange(int* a, int* b, int* c);
void swap(int* pointer1, int* pointer2);
int main(void)
{
	printf("please type a value、b value、c value:");
	scanf("%d%d%d", &a, &b, &c);
	exchange(poninter1, poninter2, poninter3);
	printf("max1=%d,max2=%d,max3=%d", *poninter1, *poninter2, *poninter3);
	return 0;
}

void exchange(int* a, int* b, int* c)
{
	if (*a < *b)
		swap(a, b);
	if (*b < *c)
		swap(b, c);
	if (*a < *b)
		swap(a, b);
}

void swap(int* pointer1, int* pointer2)
{
	int bottle;
	bottle = *pointer1;
	*pointer1 = *pointer2;
	*pointer2 = bottle;
}

C程序设计(第五版 谭浩强著)再学习第八章_第9张图片
C程序设计(第五版 谭浩强著)再学习第八章_第10张图片
在这里插入图片描述
C程序设计(第五版 谭浩强著)再学习第八章_第11张图片
C程序设计(第五版 谭浩强著)再学习第八章_第12张图片
C程序设计(第五版 谭浩强著)再学习第八章_第13张图片
例题8-6
在这里插入图片描述

//下标法
#include 
int a[10] = { 1,2,3,4,5,6,7,8,9,10 };
int main(void)
{
	for (int i = 0; i < 10; i++)
	{
		printf("%d ",a[i]);
	}
	return 0;
}
//数组名计算数组元素地址
#include 
int a[10] = { 1,2,3,4,5,6,7,8,9,10 };
int *p = a;//或者int *p = &a[0];
int main(void)
{
	for (int i = 0; i < 10; i++)
	{
		printf("%d ", *(p + i));//或者printf("%d ", *(a + i));
	}
	return 0;
}
//用指针变量指向数组元素
#include 
int a[10] = { 1,2,3,4,5,6,7,8,9,10 };
int *p = a;//或者int *p = &a[0];
int main(void)
{
	for (int i = 0; i < 10; i++)
	{
		printf("%d ", *(p++));
	}
	return 0;
}

C程序设计(第五版 谭浩强著)再学习第八章_第14张图片
例题8-7
在这里插入图片描述

#include 
int a[10] = { 0 };
int *p = a;
int main(void)
{
	printf("please type ten integer numbers:");
	for(int i = 0;i<10;i++)
		scanf("%d", p++);
	p = p - 10;
	for (int i = 0; i < 10; i++)
		printf("%d ", *(p++));
	return 0;
}

C程序设计(第五版 谭浩强著)再学习第八章_第15张图片
例题8-8、8-9
C程序设计(第五版 谭浩强著)再学习第八章_第16张图片
在这里插入图片描述

方法1

#include 
int a[10] = { 0 };
int n = 10;
int *p = a;
void swap(int *a, int *b);
int main(void)
{
	printf("please type ten integer numbers:");
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &a[i]);
	}
	for (int i = 0; i < n/2; i++)
	{
		swap(p + i, p + (n-1) - i);
	}
	for (int i = 0; i < n; i++)
	{
		printf("%d ", *(p++));
	}
	return 0;
}
void swap(int *pointer1, int * pointer2)
{
	int bottle = 0;
	bottle = *pointer1;
	*pointer1 = *pointer2;
	*pointer2 = bottle;
}

C程序设计(第五版 谭浩强著)再学习第八章_第17张图片
方法2

#include 
int a[10] = { 0 };
int n = 10; //we can change it
int* p = a;
void swap(int x[], int n);
int main(void)
{
	printf("please type ten integer numbers:");
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &a[i]);
	}
	swap(a, 10);
	for (int i = 0; i < n; i++)
	{
		printf("%d ", *(p++));
	}
	return 0;
}
void swap(int x[ ],int n)
{
	int bottle = 0;
	for (int i = 0; i < n / 2; i++)
	{
		bottle = x[i];
		x[i] = x[(n - 1) - i];
		x[(n - 1) - i] = bottle;
	}
}

C程序设计(第五版 谭浩强著)再学习第八章_第18张图片
C程序设计(第五版 谭浩强著)再学习第八章_第19张图片
例题8-10
C程序设计(第五版 谭浩强著)再学习第八章_第20张图片

#include 
int a[10] = {12,34,5,689,-43,56,-21,0,24,65};
void arrange(int* x, int number);
int main(void)
{
	arrange(a, 10);
	for (int i = 0; i < 10; i++)
	{
		printf("%d ", a[i]);
	}
	return 0;
}
void arrange(int* x, int number)
{
	for (int i = 0; i < number-1; i++)
		for (int j = 0; j < number - i - 1; j++)
		{
			int bottle = 0;
			if (*(x+j) < *(x + j+1))
			{
				bottle = *(x + j);
				*(x + j) = *(x + j + 1);
				*(x + j + 1) = bottle;
			}
		}
}

C程序设计(第五版 谭浩强著)再学习第八章_第21张图片
C程序设计(第五版 谭浩强著)再学习第八章_第22张图片
例题8-11

#include 
int a[3][4] = { 1,3,5,7,9,11,13,15,17,19,21,23 };
int main(void)
{
	printf("%p,%p\n", a,*a);
	printf("%p,%p\n", a[0], *(a+0));
	printf("%p,%p\n", &a[0], &a[0][0]);
	printf("%p,%p\n", a[1], a+1);
	printf("%p,%p\n", &a[1][0], *(a+1)+0);
	printf("%p,%p\n", a[2], *(a + 2));
	printf("%p,%p\n", &a[2], a+2);
	printf("%d,%d\n", a[1][0], *(*(a + 1) + 0));
	printf("%d,%d\n", *a[2], *(*(a + 2) + 0));
	return 0;
}

C程序设计(第五版 谭浩强著)再学习第八章_第23张图片
C程序设计(第五版 谭浩强著)再学习第八章_第24张图片
例题8-12
在这里插入图片描述
方法1

#include 
int a[3][4] = { 1,2,3,4,5,6,7,8,9,10,11,12 };
int main(void)
{
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 4; j++)
			printf("%d\t", *(*(a + i) + j));
		printf("\n");
	}
	return 0;
}

方法2

#include 
int a[3][4] = { 1,2,3,4,5,6,7,8,9,10,11,12 };
int* p;
int main(void)
{
	for (p = a[0]; p<a[0]+12; p++)
	{
		if ((p - a[0]) % 4 == 0) printf("\n");
		printf("%4d", *p);
	}
	return 0;
}

C程序设计(第五版 谭浩强著)再学习第八章_第25张图片

例题8-13
C程序设计(第五版 谭浩强著)再学习第八章_第26张图片

#include 
int a[3][4] = { 1,2,3,4,5,6,7,8,9,10,11,12 };
int (*p)[4], x=0, y=0;
int main(void)
{
	p = a;
	printf("please type row and colum:");
	scanf("%d%d", &x, &y);
	printf("a[%d][%d]=%d", x, y, *(*(p + x) + y));
	return 0;
}

C程序设计(第五版 谭浩强著)再学习第八章_第27张图片
例题8-14、8-15暂时略去
例题8-16
在这里插入图片描述

#include 
char string[] = "I love China";
int main(void)
{
	printf("%s\n", string);
	printf("%c", string[7]);
	return 0;
}

C程序设计(第五版 谭浩强著)再学习第八章_第28张图片
C程序设计(第五版 谭浩强著)再学习第八章_第29张图片
例题8-17
在这里插入图片描述

#include 
char a[] = "I love China";
char *p = &a[0];//char *p = a;
int main(void)
{
	printf("%s\n", a);
	printf("%s\n", &a[0]);
	printf("%s\n", p);
	printf("%c\n", *(p));
	printf("%c\n", *(p+2));
	return 0;
}

C程序设计(第五版 谭浩强著)再学习第八章_第30张图片

#include 
char *string = "I love China";
int main(void)
{
	printf("%s\n", string);
	return 0;
}
#include 
char *string;
int main(void)
{
	string = "I love China";
	printf("%s\n", string);
	return 0;
}

C程序设计(第五版 谭浩强著)再学习第八章_第31张图片
C程序设计(第五版 谭浩强著)再学习第八章_第32张图片
C程序设计(第五版 谭浩强著)再学习第八章_第33张图片
例题8-18、8-19
在这里插入图片描述

#include 
char string1[] = "I am student";
char string2[20];
int main(void)
{
	for (int i = 0; *(string1 + i)!='\0'; i++)
		*(string2 + i) = *(string1 + i);
	printf("string1 = %s,string2 = %s", string1, string2);
	return 0;
}

在这里插入图片描述

#include 
char string1[] = "I am student";
char string2[20];
char* pointer1 = string1;
char* pointer2 = string2;
int main(void)
{
	for (int i = 0; *(pointer1 + i)!='\0'; i++)
		*(pointer2 + i) = *(pointer1 + i);
	printf("string1 = %s,string2 = %s", pointer1, pointer2);
	return 0;
}

例题8-20
在这里插入图片描述

#include 

char string1[] = "I am student";
char string2[20];
void copy_string(char a[], char b[]);
int main(void)
{
	copy_string(string1, string2);
	printf("string1 = %s,string2 = %s", string1, string2);
	return 0;
}
void copy_string(char a[], char b[])
{
	//for (int i = 0; *(a + i) != '\0'; i++)
	//	*(b + i) = *(a + i);
	for (int i = 0; a[i] != '\0'; i++)
		b[i] = a[i];
}

C程序设计(第五版 谭浩强著)再学习第八章_第34张图片

#include 

char string1[] = "I am student";
char string2[20];
char* pointer1 = string1;
char* pointer2 = string2;
void copy_string(char* a, char* b);
int main(void)
{
	copy_string(pointer1, pointer2);
	printf("string1 = %s,string2 = %s", pointer1, pointer2);
	return 0;
}
void copy_string(char* a, char* b)
{
	for (int i = 0; *(a + i) != '\0'; i++)
		*(b + i) = *(a + i);
}

在这里插入图片描述
例题8-21

#include 
char* a = "I love China!";
int main(void)
{
	a = a + 7;
	printf("%s\n", a);
	return 0;
}

C程序设计(第五版 谭浩强著)再学习第八章_第35张图片
暂更新。。。。。。

你可能感兴趣的:(C语言,c语言,学习)