day8-c语言

1.课上代码

 1.1大小端存储问题

#include <stdio.h>
int main(int argc, const char *argv[])
{
	//字节序列/大小端存储
	int a = 0x12345678;
	char *p = &a;

	//printf("%#x\n",*p);
	
	if(*p == 0x78)
	{
		printf("小端存储\n");
	}
	else if(*p == 0x12)
	{
		printf("大端存储\n");
	}

	return 0;
}

1.2指针和字符串/字符数组

#include <stdio.h>
int main(int argc, const char *argv[])
{
	char str[] = "hello";
	char *p = str;

	int i=0;
	
	printf("%p\n",p+i);
	printf("%p\n",str+i);		
	printf("%c\n",*(p+i));
	printf("%c\n",*(str+i));
	printf("%c\n",p[i]);
	printf("%c\n",str[i]);

	printf("----------------------\n");

	char *p1 = "hello world";
	char *p2 = "hello world";

	printf("%p\n",p1);
	printf("%p\n",p2);


	return 0;
}

输出结果:

        

         a.指针和字符数组,有以下等价关系

        p + i <==> str +i  //地址        

        *(p+i) <==> *(str+i) <==> p[ i ] <==> str[ i ]   //元素

        b.两指针指向同一字符串常量,地址一样,指向的是同一片地址;

        c.通过指针简介修改字符串常量,会段错误;

 1.3字符串ab_cd_a_,写一个代码删除字符串中的_,要求使用一个字符数组

#include <stdio.h>
int main(int argc, const char *argv[])
{
	//字符串ab_cd_a 写一个代码删除字符串中的_,使用字符数组
	char s[] = "ab_cd_a";
	char *p = s;
	int len = sizeof(s);
	int i,j;
	for(i=0;i<len;i++)
	{
		if(p[i] == '_')
		{
			j = i;
			while(p[j] != '\0')
			{
				p[j] = p[j+1];
				j++;
			}
		}
	}
	puts(s);
	return 0;
}

1.4指针实现strlen、strcpy、strcat、strcmp

//strlen
#include <stdio.h>
int main(int argc, const char *argv[])
{
	char s[]="hello world";
	char *p = s;
	int count = 0;
	while(*p)
	{
		count++;
		p++;
	}
	printf("%d\n",count);
	return 0;
}

//strcpy
#include <stdio.h>
int main(int argc, const char *argv[])
{
	char s1[32] = "h";
	char s2[16] = "hello";
	printf("%s\n",s1);
	//s2拷贝到s1
	char *p1 = s1;
	char *p2 = s2;

	while(*p2)
	{
		*p1 = *p2;
		p1++;
		p2++;
	}
	printf("%s\n",s1);
	return 0;
}

//strcat
#include <stdio.h>
int main(int argc, const char *argv[])
{
	char s1[32] = "hello ";
	char s2[16] = "world";
	printf("%s\n",s1);
	printf("%s\n",s2);
	char *p1 = s1, *p2 = s2;

	//s2拼接到s1
	while(*p1)
	{
		p1++;
	}

	while(*p2)
	{
		*p1 = *p2;
		p1++;
		p2++;
	}
	*p1 = *p2; //*p1赋值\0

	printf("%s\n",s1);
	return 0;
}

//strcmp
int main(int argc, const char *argv[])
{
	char s1[32] = "hello";
	char s2[16] = "hellq";

	char *p1 = s1;
	char *p2 = s2;
	int ret = 0;

	while(*p1 !='\0' && *p2 != '\0' && *p1 == *p2)
	{
		p1++;
		p2++;
	}

	ret = *p1 - *p2;
	printf("%d\n",ret);
	return 0;
}

1.5指针实现字符数组的逆置

#include <stdio.h>
#include <string.h>
int main(int argc, const char *argv[])
{
	char s[] = "hello";
	char *p1 = s;
	int len = strlen(s);
	char *p2 = s + len -1; //指向字符串最后一个字母
	char temp;

	while(p2 > p1)
	{
		temp = *p1;
		*p1 = *p2;
		*p2 = temp;
		p1++;
		p2--;
	}

	puts(s);

	return 0;
}

1.6指针和二维数组

#include <stdio.h> 
int main(int argc, const char *argv[]) 
{ 
 int arr[2][3] = {12,90,100,80,79,0}; 
 printf("arr=%p\n",arr); 
 printf("*arr=%p\n",*arr); 
 printf("arr+1=%p\n",arr+1);  //向后偏移一行  
 printf("**(arr+1)=%d\n",**(arr+1)); 
 return 0; 
} 

1.7二级指针

#include <stdio.h> 
int main(int argc, const char *argv[]) 
{ 
 int a = 10; 
 int *p = &a;    
 int **p1 = &p; 
 int b = 100; 
 
 printf("p=%p\n",p); //a的地址
 printf("p+1=%p\n",p+1);  //从a的地址向后偏移4Byte
 printf("p1=%p\n",p1);  //p的地址
 printf("p1+1=%p\n",p1+1);  //从p的地址开始向后偏移8Byte
 
 
 *p1 = &b;   //改变p1指向的一级指针p的指向,让p去指向b  ,通过二级指针改变一级指针的指向 
 printf("%d\n",**p1); 
 return 0; 
} 

1.8数组指针的方式,求二维数组中的最大值,要求终端输入二维数组中的元素

#include <stdio.h>
int main(int argc, const char *argv[])
{
	int s[2][3] ={0};
	int (*p)[3] = s;
	int i,j;
	for(i=0;i<2;i++)
	{
		for(j=0;j<3;j++)
		{
			scanf("%d",*(p+i)+j);			
		}
	}
	int max = **p;
	for(i=0;i<2;i++)
	{
		for(j=0;j<3;j++)
		{
			if(max < *(*(p+i)+j))
			{
				max = *(*(p+i)+j);
			}
		}
	}
	printf("%d\n",max);
	return 0;
}

1.9使用mian外部传参的方式,实现简易计算器功能

#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
	int num1,num2,ret=0;
	char o;
	num1 = atoi(argv[1]);
	num2 = atoi(argv[3]);
	o = *argv[2];
	
	switch(o)
	{
	case '+':
		ret = num1 + num2;
		break;
	case '-':
		ret = num1 - num2;
		break;
	case '*':
		ret = num1 * num2;
		break;
	case '/':
		ret = num1 / num2;
		break;
	case '%':
		ret = num1 % num2;
		break;
	}
	printf("%d\n",ret);
	return 0;
}


2.使用数组指针的方式完成二维数组的输入输出

#include <stdio.h>
int main(int argc, const char *argv[])
{
	//数组指针完成二维数组的输入输出
	int s[2][3] = {0};
	int (*p)[3] = s;
	int i,j;

	for(i=0;i<2;i++)
	{
		for(j=0;j<3;j++)
		{
			scanf("%d",*(p+i)+j);  
		}
	}

	for(i=0;i<2;i++)
	{
		for(j=0;j<3;j++)
		{
			printf("%d\t",*(*(p+i)+j));
		}
	printf("\n");
	}

	return 0;
}


3.终端输入带空格的字符串,判断所含单词的个数 abc op lo --->3

#include <stdio.h>
int main(int argc, const char *argv[])
{
	char s[16] = {0}; 
	char *p = s;
	int count = 0;
	gets(s);
	
	while(*p != '\0')
	{
		if(*p != ' ' &&( *(p+1) ==' '|| *(p+1) == '\0'))
		{
			count++;
		}
		p++;
	}

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

	return 0;
}


4.整理思维导图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值