Microsoft KB Archive/39310

From BetaArchive Wiki
Knowledge Base


The ANSI C standard does not allow selective initialization of array elements

Article ID: 39310

Article Last Modified on 9/30/2005



APPLIES TO

  • Microsoft Visual C++ 1.0 Professional Edition
  • Microsoft Visual C++ 1.5 Professional Edition
  • Microsoft Visual C++ 1.51
  • Microsoft Visual C++ 1.52 Professional Edition
  • Microsoft Visual C++ 2.0 Professional Edition
  • Microsoft Visual C++ 2.1
  • Microsoft Visual C++ 4.0 Standard Edition
  • Microsoft Visual C++ 5.0 Enterprise Edition
  • Microsoft Visual C++ 6.0 Enterprise Edition
  • Microsoft Visual C++ 5.0 Professional Edition
  • Microsoft Visual C++ 6.0 Professional Edition
  • Microsoft Visual C++ 6.0 Standard Edition



This article was previously published under Q39310

SUMMARY

The ANSI C standard does not allow selective initialization of array elements with the following syntax:

   char MyArray[10] = {'a','b',,'d',,'f','g',,'i','j'};
                

This attempt at selective initialization is not correct and will generate a "Syntax Error" at compile time as follows.

error C2059: syntax error : ','

The following two declarations will compile and are valid declarations for Microsoft C version 5.1 and later:

  1. The initialization of all the elements of the array is as follows:

          char MyArray[10] = {'a','b','c','d','e','f','g','h','i','j'};
                            
  2. The initialization of the first characters in the array is as follows:

          char MyArray[10] = {'a','b','c','d','e'};


Keywords: kbhowto kbinfo kblangc KB39310