static initialization of array of objects

Posted on 2002-05-03
Last Modified: 2010-04-02
I have 2 questions

1. Just a thought,  Does it make sense in having ch -- an array of one char.

     char ch[1];

2. static initialization of array of objects
#include <iostream.h>
class ABC {
 int i;
  cout << i << endl;
};  // end of class ABC

void main()
     ABC a[5] = {1,2,3,4,5};
   // whats the problem in static initialization of
   //array of objects like this.
Question by:rv_san
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 86

Accepted Solution

jkr earned 50 total points
ID: 6988454
It will work if you make it read

ABC a[5] = { ABC(1), ABC(2), ABC(3), ABC(4), ABC(5)};

LVL 86

Expert Comment

ID: 6988458
>>Does it make sense in having ch -- an array of one char.
>>    char ch[1];

That depends on what you want to do with it...

Author Comment

ID: 6990812
ABC a[5] = {ABC(1),ABC(2),ABC(3),ABC(4),ABC(5)};

works fine. Thanks

Secondly, regarding char ch[1], I was wondering if I use "char ch[1]" as an array then the array space will be used only to store '\0'  Is there any practical situation where I must have to use "char ch[1]"  only.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 7012600

The only use for defining an array of 1 unit is if you want the compiler to treat the variable as an array when you don't actually know how big it will be.

For example:

struct somepacket {
    int     somevar;
    int     datalength;
    char    data[1];

This way, you can reference the data in you code as an array very easily.

In any other case, you can always reference the single char variable.
LVL 11

Expert Comment

ID: 7178496
Dear rv_san

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept


comment(s) as an answer.

If you think your question was not answered at all, you can explain here why you want to do this and post a request in Community support (please include this link) to refund your points. The link to the Community Support area is:


Expert Comment

ID: 7199741
Force accepted

** Mindphaser - Community Support Moderator **

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C++ get user from AD  (VS6) 7 111
is twain_32.dll cmpatible with windows 10 ? 10 274
How to convert MFC::CString to UTF8 wchar_t* 10 459
Exception thrown at 0x00007FFD5BC81F28 7 61
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question