调试技巧

first actual case of bug being found

快捷键

f9:设置/取消断点

f5:启动调试,经常用于跳到下一个断点

f10:逐过程调试,一个过程可以是一个函数,或是一个语句

f11:逐语句调试,比f10更细腻,用于进入函数内部

ctrl+f5:直接运行

更多调试技巧

一个问题

#include <stdio.h>
int main()
{
	int i = 0;
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	for (i = 0; i <= 12; i++)
	{
		arr[i] = 0;
		printf("hello\n");
	}
}// 结果是死循环

书籍推荐

《明解c语言》
《c和指针》
《c陷阱和缺陷》
《c语言深度解剖》

模拟实现字符串函数

my_strcpy

// 第一版
void my_strcpy(char* destination, char* source)
{
	while (*source != '\0')
	{
		*destination++ = *source++;		// hello的拷贝
	}
	*destination = *source;			//‘\0’的拷贝
}

int main()
{
	char arr1[] = "xxxxxxxxxx";		
	char arr2[] = "hello";
	my_strcpy(arr1, arr2);
}
//第二版
void my_strcpy(char* destination, char* source)
{
	while (*destination++ = *source++)   // '/'的ascll码为0,0为假
	{
		;
	}
}

int main()
{
	char arr1[] = "xxxxxxxxxx";
	char arr2[] = "hello";
	my_strcpy(arr1, arr2);
}
// 第三版
char* my_strcpy(char* destination, const char* source)
{
	assert(destination != NULL); 
	assert(source != NULL);			// 断言
	char* ret = destination;
	while (*destination++ = *source++)
	{
		;
	}
	return ret;
}

int main()
{
	char arr1[] = "xxxxxxxxxx";
	char arr2[] = "hello";
	printf("%s\n",my_strcpy(arr1, arr2));
}

断言:如果为真,则无事发生,如果为假,则报错

const修饰指针

const 修饰变量,这个变量就被称为常变量,不能被修改,但本质上还是变量

const a = 20;
a = 10;   // erro

const 修饰指针变量的时候, 如果const 放在* 的左边,修饰的是 * p(即p指向的内容),表示指针指向的内容,是不能通过指针来改变的.

但是指针变量本本身是可以修改的

const a = 20;
int n = 100;

const int* p = &a;

*p = 20;	// no
p = &n;		// yes

const 修饰指针变量的时候, 如果const 放在* 的右边,修饰的是指针变量p,表示指针变量p是不能被修改的

但是指针所指向的内容是可以被修改的

const a = 20;
int n = 100;

int* const p = &a;

*p = 20;	// yes
p = &n;		// no

extern 关键字

视频链接

posted @ 2023-01-30 18:20  Svdb  阅读(36)  评论(0)    收藏  举报