———–Java培训、Android培训、IOS培训、.Net培训、期待与您交流!————
本节是个人学习过程中的笔记,供初学者一起学习,欢迎大家批评指正,留言参与讨论,谢谢。
本节内容,static和const修饰的全局与局部变量,还有关于它们之间比较,使用注意事项和总结。代码如下:
#include <stdio.h>
const int a = 66;
const char b = 66;
void testConst()
{
// const int a; //莫有初始化的的常量,值未知,是系统随机赋值。
// a = 5; //对常量不可进行除了初始化以外的赋值
const int a = 77;
const char b = 65;
printf("Loacl const a = %d, b = %c\n",a,b);
//const修饰的常量,也符合作用域的限制,重名时局域优先。
}
static int s = 10;
void testStaticVariable()
{
s = 15; //可以改变值,重新赋值
printf("Address of s is:%p,s = %d\n",&s,s);
static int s = 20; //可以局部同名覆盖
printf("Address of s is:%p,s = %d\n",&s,s);
}
int times = 0;
static int* dp;
int* testLocalVariable()
{
int c = 0;
c++;
printf("Address of int c is:%p,c = %d\n",&c,c);
static int d = 0;
/*块内部定义一个静态变量,函数运行结束d依然存活并保留值,直至程序结束,
这一句定义初始化语句,仅仅是在第一次碰到时执行,第二次碰到"static int d"时,系统会优先检测,这个块区域是否还存在同类型同名的静态变量d,如果存在,系统直接不执行这个重复定义赋值的操作。所以可以起到类似计数器的作用,用于保存该函数被调用的次数。
static int d; 这两行就取不到计数器的效果,每次虽然不会再为d分配空间,但是他得值被归0咯
d = 0;
所以建议计数器,还是使用全局变量来计数,这样就很方便,而且不会产生混淆。内置的静态计数器,比较难以理解,容易出错。
*/
dp = &d;
d++;
times++;
printf("Address of static int d is:%p,d = %d\n",&d,d);
//
const int e = 100; //块内部定义一个静态变量
//e++;错误写法,千万不能只是复制了事。
printf("Address of const int e is:%p,e = %d\n",&e,e);
//return d;//直接返回d的地址?变成返回指针的函数?
return &d;
}
void testPoint()
{
//int *p1 = 1;//这句左右类型不同,自然出错。
//int *p1 = &1;//这一句,数字1还没分配地址
// int *p1; 这也是错误的,貌似都没地址
// *p1 = 1;
int temp1 = 11;
int *p1 = &temp1;
printf("Address of p1 is:%p,*p1 = %d\n",p1,*p1);
temp1 = 12;
printf("Address of p1 is:%p,*p1 = %d\n",p1,*p1);
//&temp1++; 变量地址值是一个常量,不能修改
//&temp1 = p1;
(*p1)++;
printf("Address of p1 is:%p,*p1 = %d\n",p1,*p1);
printf("Address of temp1 is:%p,*temp1 = %d\n",&temp1,temp1);
p1++;//下面两个输出就不同咯,p1抛弃咯temp变量。
printf("Address of p1 is:%p,*p1 = %d\n",p1,*p1);
printf("Address of temp1 is:%p,*temp1 = %d\n\n",&temp1,temp1);
static int temp2 = 21; //temp2也必须用static修饰,因为p2是指向静态int变量
static int *p2 = &temp2;
printf("Address of p2 is:%p,*p2 = %d\n",p2,*p2);
temp2 = 22;
printf("Address of p2 is:%p,*p2 = %d\n",p2,*p2);
(*p2)++;
printf("Address of p2 is:%p,*p2 = %d\n",p2,*p2);
printf("Address of temp2 is:%p,*temp2 = %d\n",&temp2,temp2);
p2++;//下面两个输出就不同咯,p2抛弃咯temp2变量。
printf("Address of p2 is:%p,*p2 = %d\n",p2,*p2);
printf("Address of temp2 is:%p,*temp2 = %d\n\n",&temp2,temp2);
int temp3 = 31;
const int* p3 = &temp3; //没有要求temp3一定也为const类型
printf("Address of p3 is:%p,*p3 = %d\n",p3,*p3);
//(*p3)++; 系统报错,说*p3只读,也就是const的不可改变
temp3 = 32;
printf("Address of p3 is:%p,*p3 = %d\n",p3,*p3); //temp3主动去改变,是可以的
p3++;
printf("Address of p3 is:%p,*p3 = %d\n",p3,*p3);
printf("Address of temp3 is:%p,*temp3 = %d\n",&temp3,temp3);
//所以const型指针变量,不能靠指针去修改所指的目标值,但是如果指向的目标自己改变,那么我们只能跟着改变。当然如果两边都是const型,那么大家都不能变
}
int main()
{
testConst();
printf("overall const a = %d, b = %c\n",a,b);
printf("Address of s is:%p,s = %d\n",&s,s);
testStaticVariable();
int return_d = 0;
int* return_d_address = 0;
for (int i=3; i<6;i++) {
//return_d = testLocalVariable();//返回数值,和返回地址两种方式
return_d_address = testLocalVariable();
}
//四种方法返回函数执行次数;
//printf("Address of return_d is:%p,return_d = %d\n",&return_d,return_d);
printf("Address of return_d_address is:%p,return_d_address = %d\n",return_d_address,*return_d_address);
printf("Address of times is:%p,times = %d\n",×,times);
printf("Address of dp is:%p,*dp = %d\n\n",dp,*dp);
testPoint();
return 0;
}
程序运行结果如下: