Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 15th, 2008, 10:55 PM   #1
kurt
Programmer
 
Join Date: Oct 2005
Posts: 63
Rep Power: 3 kurt is on a distinguished road
Init array with a non-zero value

This is embarrasing, but I've always thought:

int variable[10] = {9};

will init all items to 9. But it does not.

Any cool way to init to a non-zero value besides a for-loop?

Thanks in advance.
kurt is offline   Reply With Quote
Old Apr 15th, 2008, 11:16 PM   #2
mbd
Programmer
 
Join Date: Nov 2007
Posts: 86
Rep Power: 1 mbd is on a distinguished road
Re: Init array with a non-zero value

c++ Syntax (Toggle Plain Text)
  1. #include <algorithm>
  2.  
  3. ...
  4.  
  5. int variable[10];
  6. std::fill_n(variable, 10, 9);

still just a loop under the hood
mbd is offline   Reply With Quote
Old Apr 16th, 2008, 6:57 AM   #3
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
Re: Init array with a non-zero value

For smaller arrays, you can do this
c++ Syntax (Toggle Plain Text)
  1. int variable[5] = {9,9,9,9,9};
OpenLoop is offline   Reply With Quote
Old Apr 16th, 2008, 3:48 PM   #4
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 887
Rep Power: 4 lectricpharaoh will become famous soon enough
Re: Init array with a non-zero value

Quote:
Originally Posted by kurt
This is embarrasing, but I've always thought:

int variable[10] = {9};

will init all items to 9. But it does not.
When you initialize an array like that, it will initialize all elements you specify to the values you give, and any remaining elements will be initialized to zero (for fundamental types) or with the default constructor (for objects). If the array does not have a subscript, then the number of elements is taken from the number of values you provide:
C++ Syntax (Toggle Plain Text)
  1. // all set to 9
  2. int array1[10] = {9, 9, 9, 9, 9, 9, 9, 9, 9, 9};
  3.  
  4. // first three set to 9, rest set to 0
  5. int array2[10] = {9, 9, 9};
  6.  
  7. // five elements, all set to 9
  8. int array3[] = {9, 9, 9, 9, 9};
__________________
A man's knowledge is like an expanding sphere, the surface corresponding to the boundary between the known and the unknown. As the sphere grows, so does its surface; the more a man learns, the more he realizes how much he does not know. Hence, the most ignorant man thinks he knows it all. - L. Sprague de Camp
lectricpharaoh is offline   Reply With Quote
Old Apr 16th, 2008, 3:51 PM   #5
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 288
Rep Power: 4 Klarre is on a distinguished road
Re: Init array with a non-zero value

memset is a function that can be pretty handy sometimes. Even thought it may not function as expected with your "int" it is good to know about it when you are using char's.
http://www.cplusplus.com/reference/c...ng/memset.html
__________________
http://www.klarre.se
Klarre is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamic array help quickster12 C++ 4 Nov 29th, 2007 11:52 PM
problem processing file into a char array csrocker101 C++ 1 May 8th, 2007 11:50 PM
changing size of an array Eric the Red Java 3 Apr 3rd, 2006 8:19 PM
Installing IPB 2.03 bh4575 Other Web Development Languages 0 Apr 23rd, 2005 2:36 AM
Converting 1-dimensional array to 2-dimensional array Tazz_Mission_13 Java 6 Apr 8th, 2005 11:58 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:21 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC