c语言 局部 数组,C语言中全局数组和局部数组的问题.doc

C语言中全局数组和局部数组的问题

语言中全局数组和局部数组的问题

今天同学遇到一个在语言中全局数组和局部数组的问题,卡了许久,我也没有第一时间看出问题,现在把问题梳理一下,并给出解决方案。

问题描述:

在全局声明的数组与在局部声明的数组有着不同的效果。

首先来看一个程序:

代码如下:

#

#

# X 10

[X];

()

{

;

[X];

*=( *)(X * ());

(“ : “);

(=0; (“% “,[]);

(“ : “);

(=0; (“% “,[]);

(“ : “);

(=0; (“% “,[]);

(“ ”);

();

1;

}

编译运行结果:

程序主要功能是打印字符数组的码语言中全局数组和局部数组的问题。可以发现全局数组和动态生成的数组有着相同的结果,而局部声明的数组确被分配了随机的数值,也许这就是问题的所在。

解决方案:

代码如下:

#

#

# X 10

[X]={0};

()

{

;

[X]={0};

*=( *)(X * ());

(“ : “);

(=0; (“% “,[]);

(“ : “);

(=0; (“% “,[]);

(“ : “);

(=0; (“% “,[]);

(“ ”);

();

1;

}

运行结果:

在数组的初始化中,假设初始化的数值个数小于数组的大小,则全部用0来填充语言中全局数组和局部数组的问题。这里通过初始化一个值,就可以给数组一个确定的结果。

(在不同系统和不同的编译器可能会出现不同的结果)

还有一个小问题就是语言中空格的问题,看下面的程序。

代码如下:

#

#

# X 10

()

{

;

[X]={0};

();

(“ : “);

(=0; (“% “,[]);

(“ ”);

1;

}

在这里,我输入了” ”(三个空格+),打印的结果如上图语言中全局数组和局部数组的问题。

中前三个记录了空格的码,即32。

中后面没有使用的空间依然是0.

daily morning exercises or class-break setting-up exercise and discipline inspection and appraisal of public signs;2, daily cleaning and inspection;3, the daily student school appearance, dress, wearing school card check;4, the daily duty guide to the leadership class discipline three rounds.Weekly moral education work:L, the flag raising ceremony and the "national flag" speech under the flag raising ceremony held on Monday";2, every week a class meeting, the meeting;3, every week "two exercises, cleaning and hygiene, behavior norms, attendance" situation published cloth appraisal, and the issue of the code of conduct;;4, weekly work week.Monthly moral education work:1, at least once a month, the theme of education activities, to patriotism education, behavior norms of education as the focus, combined with major retreat day.2, the monthly c

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值