数组初始化之间的区别 [英] Difference between Array initializations

查看:50
本文介绍了数组初始化之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅以下声明:

char a[5]="jgkl"; // let's call this Statement A
char *b="jhdfjnfnsfnnkjdf"; // let's call this Statement B , and yes i know this is not an Array
char c[5]={'j','g','k','l','\0'}; // let's call this Statement C

现在,语句A和C之间有什么区别吗?我的意思是两个都应该在堆栈上,不是吗?只有b会位于静态位置.

Now, is there any difference between Statements A and C? I mean both should be on Stack dont they? Only b will be at Static location.

那么,在整个程序生命周期中,使"jgkl"不存在于静态位置吗?由于它应该是只读/常量?请澄清.

So wouldn't that make "jgkl" exist at the static location for the entire life of the program? Since it is supposed to be read-only/constant? Please clarify.

推荐答案

如果a []是静态的,则c []也是静态的-两者是等效的,而且都不是字符串文字.同样可以声明这两者,以便它们位于堆栈上-它取决于声明它们的位置和方式,而不是用来指定其内容的语法.

If a[] is static then so is c[] - the two are equivalent, and neither is a string literal. The two could equally well be declared so that they were on the stack - it depends where and how they are declared, not the syntax used to specify their contents.

这篇关于数组初始化之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆