C Array/String Initialization

A question came up in lab today about what happens with the following C line at compilation and at run time:

People doubted that the effect of this line would be to reserve space in memory for 80 char and initialize the first six of them with:

So, in order to settle this dispute, I wrote this short C program:

 

This little, seemingly silly program was enough to test some assumptions. The output it produced was:

I also took a peek at the symbol table in this program with nm:

My interpretation from these results is that the line

does indeed allocate (in the data segment) space for 80 char and initializes the first six to:

In fact, what it does is to initialize the first 5 positions with ‘h’, ‘e’, ‘l’, ‘l’, ‘o’ and all the remaining 75 elements of the array with byte value 0. This observation is consistent with what I found at:

http://stackoverflow.com/questions/201101/how-to-initialize-an-array-in-c

Print Friendly

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">