Day19.C提高3

Day19.C提高3

一、calloc和realloc

//开辟一块内存空间,calloc自动将分配的内存置零 格式:
//calloc(所需内存单元数量,每个内存单元的大小)
char* arr = (char*)calloc(10, sizeof(char));
if (arr != NULL)
{
	free(arr);
	arr = NULL;
}
/*
realloc 格式:realloc(void* ptr,size_t size);
ptr:之前用malloc或者calloc分配的内存地址,若此参数为NULL,则和malloc与calloc功能一致
size:重新分配的内存大小
realloc重新分配malloc或者calloc函数在堆中分配内存大小,
如果指定的地址后面有连续的内存空间,那么就会在已有的基础上增加内存,
如果指定的地址后面没有空间,那么realloc会重新分配新的连续内存,把旧内存的值拷贝到新的内存,同时释放旧内存。
*/
char* arr = realloc(arr, sizeof(char) * 20);

二、实现strstr

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

//查找子字符串第一次出现的位置
char* myStrStr(const char* str,const char* substr)
{
	const char* mystr = str;
	const char* mysub = substr;
	while (*mystr)
	{
		if (*mystr != *mysub)
		{
			++mystr;
			continue;
		}
		//创建临时变量
		char* temp_mystr = mystr;
		char* temp_mysub = mysub;

		while (*temp_mysub)
		{
			if (*temp_mysub != *temp_mystr)
			{
				break;
			}
			++temp_mystr;
			++temp_mysub;
		}
		if (!*temp_mysub)
		{
			return mystr;
		}
		

		++mystr;
	}
	return NULL;
}

int main(void)
{
	char* arr1 = "abcdef12564gdfpoiq";
	char* arr2 = "64g";
	char* result = (char*)myStrStr(arr1, arr2);
	printf("%s\n", result);



	return 0;

}

三、指针易错点

//操作某块内存的时候,这块内存一定要是合法的(自己申请的并且还没有释放掉的)
char* p = malloc(64);

++p;
if(p != NULL)
{
	free(p);
	p = NULL;
}

四、const使用

//规避地址传递的副作用(在使用对象指针的情况下,有可能意外修改数据)

示例:
	//容易出现问题的形式
	void fun_1(struct Struct_Name* Struct_Name_1){}
	//不容易出现问题的形式
	void fun_1(const struct Struct_Name* Struct_Name_1){}

五、二级指针练习(读文件)

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int getFileLines(FILE* file)
{
	if (!file)
		return -1;
	char buf[1024] = { 0 };
	int lines = 0;
	while (!feof(file))
	{
		fgets(buf, 1024, file);
		++lines;
	}
	//将文件光标移动到文件开头
	fseek(file, 0, SEEK_SET);
	return lines;
}

void readFileDate(char** EachLineP,FILE* file,int lines)
{
	if (EachLineP == NULL)
		return;
	if (file == NULL)
		return;
	if (lines <= 0)
		return;
	int index = 0;
	char buf[1024] = { 0 };
	int lens = 0;
	while (!feof(file))
	{
		fgets(buf, 1024, file);
		lens = strlen(buf) + 1;
		char* p = malloc(sizeof(char) * lens);
		strcpy(p,buf);
		EachLineP[index++] = p;
		memset(buf, 0, 1024);
	}
	fclose(file);
	file = NULL;
	
	
}

void showFileDate(char** counts, int lines)
{
	//打印文件
	for (int i = 0; i < lines; ++i)
	{
		printf("%d行%s", i + 1, counts[i]);
	}
}

void freeFilePlace(char** counts, int lines)
{
	for (int i = 0; i < lines; ++i)
	{
		if (counts[i] != NULL)
		{
			free(counts[i]);
			counts[i] = NULL;
		}
	}
	if (counts != NULL)
		free(counts);
		counts = NULL;
}

void test01()
{
	//以读的形式打开文件
	FILE* file = fopen("test.txt", "r");
	if (!file)
		return -1;
	
	//获取文件行数
	int lines = getFileLines(file);
	
	//给每行的指针开辟内存
	char** EachLineP = malloc(sizeof(char*) * lines);
	
	//读取文件
	readFileDate(EachLineP, file, lines);
	
	//显示文件内容
	showFileDate(EachLineP, lines);
	
	//释放空间
	freeFilePlace(EachLineP, lines);

}

int main(void)
{
	test01();

	return 0;
}

六、位运算

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

//按位取反 ~
void test01()
{
	int number = 2;// 010 -> 101(补码) -> 111(原码)
	printf("~number:%d\n", ~number);
}

//位与& (可以把某一位置一)
void test02()
{
	int number = 335;
	if (number & 1)
	{
		printf("该数字为奇数");
		return;
	}
	printf("该数字为偶数");
}

//位或 | (可以把某一位清零)
void test03()
{
	int number1 = 3;
	int number2 = 5;
	printf("%d", number1 | number2);
}

//异或 ^
void test04()
{
	int num1 = 5;
	int num2 = 9;
	printf("num1:%d num1:%d\n", num1, num2);

	num1 = num1 ^ num2; //A^B=R A=R
	num2 = num1 ^ num2;//B^R=A  B=原来的A
	num1 = num1 ^ num2;//R^A=B  A=原来的B
	printf("num1:%d num1:%d\n", num1, num2);
}

//移位运算符
//左移空出来的位用0来补齐,溢出的位丢掉(左移几位就相当于乘以2的多少次方)
//右移空出来的位,对于 unsigned 类型数用0来补齐,对于有符号类型,结果依赖于机器,空出来的位可能用0来填充,或者使用符号(最左端)位的副本填充。	溢出的位丢掉(如果数据非负,右移几位就相当于除以2的多少次方)
void test05()
{
	int num1 = 20;
	printf("num1 << 2: %d\n", num1 <<= 2);
	printf("num1 << 2: %d\n", num1 >>= 1);
}


int main(void)
{
	//test01();
	//test02();
	//test03();
	//test04();
	test05();


	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值