绝不要进行两层间接非const指针赋值给const指针

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
	int			*p1;
	int *		*pp1;
	const int *	*pp2;

	const int n = 13;
	printf("起初const int n = %d
", n);
	pp1			= &p1;
	pp2			= pp1;
	*pp2		= &n; /*  间接的使p1=&n  */
	**pp1		= 10;
	printf("二层间接赋值后const int n = %d
", n);
	system("pause");

	return 0;
}

编译命令:

gcc -Wall -ansi -pedantic a.c

输入a.exe后运行查看结果

可以发现编译的时候有个警告,运行时常量n的值已经被更改。

原文地址:https://www.cnblogs.com/xxNote/p/4019859.html