Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > incomplete initialization of array

Thread Tools

incomplete initialization of array

J. J. Farrell
Posts: n/a
Harald van Dijk wrote:
> On Oct 1, 4:53 am, "J. J. Farrell" <(E-Mail Removed)> wrote:
>> Mark wrote:
>>> #define MAX_LEN 10
>>> char name[MAX_LEAN + 1] = { '\0', };

>> If this is the actual code rather than a typo, then the array is only
>> one element in length. Is that what has confused you?

> I don't understand what you mean here. Yes, MAX_LEN and MAX_LEAN are
> spelt differently, but that should result in a compiler error, not an
> array of a different length.

Apologies, I must have been asleep when posting. My mind had wandered
off into the messy areas of preprocessor constant expressions involving
macros which may or may not be defined, and may be defined as empty. I
had to fight with that a lot in my first C project, and it left deep
scars. I don't know which genius added "defined" to C89, but I remember
wanting to buy him a pint; it made that sort of thing much easier.

My comment would have made sense if there were a

#define MAX_LEAN

or equivalent before that code.
Reply With Quote
J. J. Farrell
Posts: n/a
James Kuyper wrote:
> On 10/01/2011 07:55 PM, BartC wrote:
>> "Joe Pfeiffer" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> "BartC" <(E-Mail Removed)> writes:
>>>> I don't know about the standard. But some experimentation shows only
>>>> the first element is set. The rest seem to be zeros.
>>> Well, yes -- if you specify any at all, the ones you specify are set to
>>> whatever you want. The others are all set to 0.

> ...
>> int a[5]={1}; (1,0,0,0,0) not (1,1,1,1,1) as might be assumed.

> You're right, that assumption is incorrect. But I didn't realize that's
> what you were talking about. Is that what Mark was talking about, too?

Hard to tell since the example he gave would have had the same result
for both possibilities. For the original example, at least, "not
working" couldn't mean he'd made the wrong assumption described here.
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
initialization of array as a member using the initialization list aaragon C++ 2 11-02-2008 04:57 PM
array initialization in initialization list. toton C++ 5 09-28-2006 05:13 PM
Initialization of non-integral type in initialization list C++ 6 12-11-2005 09:51 PM
Initialization via ctor vs. initialization via assignment Matthias Kaeppler C++ 2 07-18-2005 04:25 PM
Default Initialization Vs. Value Initialization JKop C++ 10 09-22-2004 07:26 PM