hello all , i have to initialize the array of stucture singleIMEIarr to null , using NULL doesnt work , when i assign {'\0'} it doesnt execute. any suggestions at to where im wrong ?

struct singleIMEI 
     {
       string planname;
       string plandescription;
       string scriptexec;
       string startdate;
       string starttime;
       string stopdate;
       string stoptime;
       string delayinterval;
       string repeatdelay;
       string notonweekend;
       string activate;
       string temptime;
       string tempdate;
     };      //VECTOR IMPLEMENTATION PENDING    
     singleIMEI singleIMEIarr[30] = {'\0'};

Recommended Answers

All 9 Replies

Of course, you can't initialize array of singleIMEI elements to "null" because no such value as '\0' or 0 of singleIMEI type. However an array of class objects initializes by default class constructor in C++ (did you remember that a struct is a class with all public members in C++ ? ). Obviously, the default singleIMEI constructor builds a stucture object with empty string members. Therefore no need in any special initialization list for arrays of this structure. After declaration you always get an array of structures with all empty (zero length) string members.

That's why default constructors were provided for in C++ ;)

singleIMEI singleIMEIarr[30] = {'\0'}; That's a NULL char. ;) By definition arrays in C NOT dynamically allocated are always completely 0.

NULL char in C++ is a senseless word pair (don't forget case-sensitive vocabulary ;)). NULL denotes a generic pointer constant. What's a wonderful chimere - pointer char!

The second sentence is wrong in C++ (C and C++ are different languages ;)). Only allocated by operator new objects are named as dynamically allocated. However automatically (not dynamically) allocated arrays of POD objects are not initialized by default.

Once more: arrays of class objects with zero bits contents are absolutely unusable in a general case. That's why elements of such arrays always initialized by base type default constructor.

Eh yeah, ArkM's right in every way. Ignore my previous post.

In this case(where you need to initialize a structure to a arbitrary value), you need to write a constructor for that.
Constructors are part of Classes. But in C++, even structures can have constructors,destructor member function etc.. Loosely speaking, structure is as same as a class except that the members are public by default.
So you should perhaps write a constructor for your structures.
You may also overload the copy constructor, assignment operator etc etc etc.
But if you wanna do it the 'C' style( means not using any thing as constructor or destructor) you perhaps ask a C guru. I would suggest that you should make function which will take the array of singleIMEI and initiallisize it to zero(i.e. Null. Note that C++ guarantee NULL to be zero. So saying that the function should initialize it to NULL is same as saying the function initialize it to zero).

Note that all your members are c++ string, you don't require write a constructor explicitly. As the constructors of each string will initialize a null string(empty string)

siddhant3s, NOBODY can initialize to zero an array of structures with std::string members, even C guru can't do that ;)...
The C++ DOES NOT garantee that null pointer value (NULL) to be zero! Moreover, it's a very dangerous (extremely non-portable) delusion.

To set up a handful of records, perhaps for a little hard-coded testing, you can, of course, do this:

// Initializing first 3 fields of first 2 records
    singleIMEI a[30] = {{"00","01","02"},
                        {"10","11","12"}};

ArkM, Read the first line of my post :-
In this case(where you need to initialize a structure to a arbitrary value), you need to write a constructor for that...
That means that I was talking in general. While my last sentence,("Note that all your members are c++ string, you don't require write a constructor explicitly. As the constructors of each string will initialize a null string(empty string)") was a specific discussion to the OP's problem.

>>The C++ DOES NOT garantee that null pointer value (NULL) to be zero
Who told you this. Not me at least.
What I said is that:
The NULL constant is same as zero in C++. (Ref: Stroustrup Homepage: http://www.research.att.com/~bs/bs_faq2.html#null)
Of course, a pointer holding a value NULL, when dereferenced, will NOT in general have a value zero.

int * p=NULL;
std::cout<<*p;//Will NOT print 0. Is a VERY BAD practice

hello all , i have to initialize the array of stucture singleIMEIarr to null , using NULL doesnt work , when i assign {'\0'} it doesnt execute. any suggestions at to where im wrong ?

struct singleIMEI 
     {
       string planname;
       string plandescription;
       string scriptexec;
       string startdate;
       string starttime;
       string stopdate;
       string stoptime;
       string delayinterval;
       string repeatdelay;
       string notonweekend;
       string activate;
       string temptime;
       string tempdate;
     };      //VECTOR IMPLEMENTATION PENDING    
     singleIMEI singleIMEIarr[30] = {'\0'};

You don't have to do that at all because std::string class will automatically create empty string when initially declared. If you want to re-initialize the structure to empty strings after it has been used, such as in a loop, then create an initializer method for that structure, something like below. In the loop your program just calls initialize() method to reinitiailze the structure.

struct singleIMEI 
     {
       string planname;
       string plandescription;
       string scriptexec;
       string startdate;
       string starttime;
       string stopdate;
       string stoptime;
       string delayinterval;
       string repeatdelay;
       string notonweekend;
       string activate;
       string temptime;
       string tempdate;

singleIMEI() { intialize(); }

void initialize()
{
    planname = "";
    plandescription = "";
 // etc. etc for each string
}
     };      //VECTOR IMPLEMENTATION PENDING
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.