6 Replies - 17363 Views - Last Post: 11 January 2013 - 11:33 PM Rate Topic: -----

#1 Elvang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-January 10

How to initialize variable sized array

Post icon  Posted 25 January 2010 - 05:39 PM

How would I initialize a variable-sized array to all '0's/'false's in C++? I know I can just use a for loop to iterate over the array, but I'd rather know why I can't initialize it like non variable-sized objects.
// Bare minimum to generate compiler error:
// variable-sized object `array' may not be initialized
#include <iostream>

int main(int argc, char** argv) {
	bool array[argc] = {false};
	// bool array[17] = {false}; // Legal
	return 0;
}


PS: I currently make sure it won't try to create a size 0 array, but what would happen if it did attempt to?

Is This A Good Question/Topic? 0
  • +

Replies To: How to initialize variable sized array

#2 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: How to initialize variable sized array

Posted 25 January 2010 - 06:18 PM

if you declare the array as static all elements are initialized to zero

but I am not sure
please check it
Was This Post Helpful? 0
  • +
  • -

#3 Elvang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-January 10

Re: How to initialize variable sized array

Posted 25 January 2010 - 06:33 PM

Declaring it static causes the compiler to list the following errors in addition to the previous error:
  • storage size of `array' isn't constant
  • size of variable 'array' is too large

Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,564
  • Joined: 09-June 09

Re: How to initialize variable sized array

Posted 25 January 2010 - 06:53 PM

memset

char arr[20];
//initialize all to NULL
memset(arr,'\0',sizeof(arr));

//*****
int arr[20;
//initialize all to zero
memset(arr,0,sizeof(arr));


This post has been edited by ImaSexy: 25 January 2010 - 06:53 PM

Was This Post Helpful? 0
  • +
  • -

#5 ubunta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-September 12

Re: How to initialize variable sized array

Posted 11 January 2013 - 10:35 PM

View PostElvang, on 25 January 2010 - 06:33 PM, said:

Declaring it static causes the compiler to list the following errors in addition to the previous error:
  • storage size of `array' isn't constant
  • size of variable 'array' is too large

also it will not work for float arrays
Was This Post Helpful? 0
  • +
  • -

#6 indelako  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-January 13

Re: How to initialize variable sized array

Posted 11 January 2013 - 11:30 PM

you need to make the data type of the array and the size of it. if you want, you can also initialize the content of the array.

Example:

char name[2]={"del" , "danny" };
it us not allowed that your array size is less that what you initialized
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10669
  • View blog
  • Posts: 39,626
  • Joined: 27-December 08

Re: How to initialize variable sized array

Posted 11 January 2013 - 11:33 PM

Please avoid necroposting. Topic closed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1