473,785 Members | 3,349 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Initializing Int Array With a Non-Zero Value

Hi, Everyone!

Does anyone know how to initialize an int array with a non-zero number?
Thank You Very Much.

Truly Yours, Simon Dexter

Jul 23 '05
13 27172
"Rick N. Backer" <ke********@NsO hSaPw.cAaM> wrote in message
news:72******** *************** *********@4ax.c om
On Wed, 13 Jul 2005 15:46:54 +1000, "John Carson"
<jc************ ****@netspace.n et.au> did courageously avow:
"Rick N. Backer" <ke********@NsO hSaPw.cAaM> wrote in message
news:if******** *************** *********@4ax.c om
On 12 Jul 2005 21:46:19 -0700, si******@yahoo. com did courageously
avow:

Hi, Everyone!

Does anyone know how to initialize an int array with a non-zero
number?

int intArr[n1] = {x1}; // declaration and initialization

n1 can be any positive integer value and x1 can be any legitimate
value; number, char, pointer to another array, etc.. I believe you
don't need to specify all the elements in the list. That said, I
trust I will be corrected if I'm wrong
Your code has the effect of initializing the first element of the
array to x1 and the rest to zero. You also can't use pointer values
without a cast.


Where is this pointer you speak of?


You say: "x1 can be any legitimate value; number, char, pointer to another
array, etc". Thus the "pointer to another array" is the pointer that I speak
of. x1 cannot be a pointer without using a cast.
I see an int array being initialized to hold n1 elements of which the
first will be x1 and the rest zero as you say. If you had all my post
here, you would see I also explained how to declare all the members at
once if the OP so wished, how to declare an array without
initialization and then initialize a single element later, and also
suggest how it could be done in a for loop. Why are you centering on
one item and not the whole post?


The OP asked how to initialize an array with a non-zero number. I took this
to mean initialize the *whole* array with a non-zero number and so I took
your answer to be a claim that

int intArr[n1] = {x1};

initialized each element in the array to x1. The fact that you also gave
alternative methods of initialization was not inconsistent with this
interpretation.

If my interpretation was incorrect, I apologize.

--
John Carson

Jul 23 '05 #11


upashu2 wrote:
how to initialize an int array with a non-zero number?
John Carson .....
first element of the array to x1 and the rest to zero
if we write, int a[20]; it leaves the whole array uninintialized. It
doesn't initalize array with 0's at all. after all it is c++, not java
or vb.
That's true, but not what John presented. He initialized the first
value. The rest of the array will be default initialized.
if we write int a[20]={ 1,2};
then it will initialize only first two elements, and leave all others
uninitialized.
You are incorrect. The rest of the array will be filled with 0.
Still if you want to initialize an array with 0's, you have to write it
expilicitly.


Wrong.


Brian

Jul 23 '05 #12
[some further thoughts]

"Rick N. Backer" <ke********@NsO hSaPw.cAaM> wrote in message
news:72******** *************** *********@4ax.c om
On Wed, 13 Jul 2005 15:46:54 +1000, "John Carson"
<jc************ ****@netspace.n et.au> did courageously avow:
"Rick N. Backer" <ke********@NsO hSaPw.cAaM> wrote in message
news:if******** *************** *********@4ax.c om
On 12 Jul 2005 21:46:19 -0700, si******@yahoo. com did courageously
avow:

Hi, Everyone!

Does anyone know how to initialize an int array with a non-zero
number?

int intArr[n1] = {x1}; // declaration and initialization

n1 can be any positive integer value and x1 can be any legitimate
value; number, char, pointer to another array, etc.. I believe you
don't need to specify all the elements in the list. That said, I
trust I will be corrected if I'm wrong
Your code has the effect of initializing the first element of the
array to x1 and the rest to zero. You also can't use pointer values
without a cast.


Where is this pointer you speak of?
I see an int array being initialized to hold n1 elements of which the
first will be x1 and the rest zero as you say. If you had all my post
here, you would see I also explained how to declare all the members at
once if the OP so wished, how to declare an array without
initialization and then initialize a single element later, and also
suggest how it could be done in a for loop.


You didn't say at *any* point in your post that

int intArr[n1] = {x1};

would set all elements after the first to zero, so it was surely worth
pointing it out so the OP would know what to expect.
Why are you centering on one item and not the whole post?


I was not writing a review. It is perfectly legitimate to focus on the one
part of a post that is in need of clarification.

--
John Carson

Jul 23 '05 #13
On Thu, 14 Jul 2005 03:25:54 +1000, "John Carson"
<jc************ ****@netspace.n et.au> did courageously avow:
[some further thoughts]

"Rick N. Backer" <ke********@NsO hSaPw.cAaM> wrote in message
news:72******* *************** **********@4ax. com
On Wed, 13 Jul 2005 15:46:54 +1000, "John Carson"
<jc************ ****@netspace.n et.au> did courageously avow:
"Rick N. Backer" <ke********@NsO hSaPw.cAaM> wrote in message
news:if******** *************** *********@4ax.c om
On 12 Jul 2005 21:46:19 -0700, si******@yahoo. com did courageously
avow:

> Hi, Everyone!
>
> Does anyone know how to initialize an int array with a non-zero
> number?

int intArr[n1] = {x1}; // declaration and initialization

n1 can be any positive integer value and x1 can be any legitimate
value; number, char, pointer to another array, etc.. I believe you
don't need to specify all the elements in the list. That said, I
trust I will be corrected if I'm wrong

Your code has the effect of initializing the first element of the
array to x1 and the rest to zero. You also can't use pointer values
without a cast.
Where is this pointer you speak of?
I see an int array being initialized to hold n1 elements of which the
first will be x1 and the rest zero as you say. If you had all my post
here, you would see I also explained how to declare all the members at
once if the OP so wished, how to declare an array without
initialization and then initialize a single element later, and also
suggest how it could be done in a for loop.


You didn't say at *any* point in your post that

int intArr[n1] = {x1};

would set all elements after the first to zero, so it was surely worth
pointing it out so the OP would know what to expect.


I have no problem and readily concede that point and, truthfully,
hadn't considered that aspect. I may have boiled the question down to
much in trying to simplify it and my response.
Why are you centering on one item and not the whole post?


I was not writing a review. It is perfectly legitimate to focus on the one
part of a post that is in need of clarification.


Oh how I hate first encounters :-). I am used to people only
applying to one point and will concede this also and stand now
unoffended. I am just used to people interjecting in line and
returning the original document intact so context of the discussion
can be maintained. In this instance I ass-u-me'd, incorrectly, that
you hadn't paid attention to the rest of the post. My apologies.
Ken Wilson

Amer. Dlx. Tele, Gary Moore LP, LP DC Classic w/P90s,
Jeff Beck Strat, Morgan OM Acoustic,
Rick 360/12, Std. Strat (MIM), Mesa 100 Nomad,
Mesa F-30

"Goodnight Austin, Texas, wherever you are."
Jul 23 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2838
by: trying_to_learn | last post by:
I'm learning consts in C++ and the book says that u have to initialize non-static consts inside the constructor initializer list, however "const string* stack" isn't initialized in the constructor initializor list,instead its initialized inside the constructor main body using memset.I dont understand this,why isnt this uniform class StringStack { static const int size = 100; const string* stack; int index;
2
3414
by: Andrew Ward | last post by:
The following program compiles and runs fine on my compiler (vc7.1): #include <memory> using namespace std; class X {}; auto_ptr<X> foo() {
3
5880
by: Bill Pursell | last post by:
I've found myself wanting to do this: int *x = {1,2,3,4,5}; Obviously, I can't do that. I can certainly non-portably hack it like int *x = (int *)"\x01\x00\x00\x00\x02\x00...", but that's the worst idea since W's last one. Or I can do: int a = {1,2,3,4,5}; int *x=a;
4
4406
by: jayharris | last post by:
I'm having a ton of trouble initializing a multi-dimensional array inside a constructor, largely because I don't know the size of the array until runtime. I have a class that looks like this: class MyClass { public: const int size; MyClass( const int ); };
7
4068
by: nk | last post by:
Hi, I'm a newbie on this language. I would be very happy if you help me about the following issue: The code below, reads some names(strings), stores them, and stores the addresses in the pointer array, and writes them out. But it fails and exits the program. I guess that it's about initializing the array but i couldn't find a way to make it ok....
13
2280
by: John | last post by:
Is this a valid C++ program that will not crash on any machine? #include <iostream> using namespace std; int main( void ) { int i; cin >i; double X; X = 1123;
6
3374
by: alacrite | last post by:
If I have this situation class X { Z z; Y y; }; Class X has two objects of type Z and Y. How do I initialize z and y with non default constructors?
2
3053
by: Pavan | last post by:
Hi, I need to create a consant array of larze size but however its elements can be calculated using an equation, say for example I need an int arry of 20 elements where each element will be arr = 2 + (i*i) But I want arry to be constant. How can I declare such a constant array without actually defining all the elements?
10
1911
by: Jason Doucette | last post by:
Situation: I have a simple struct that, say, holds a color (R, G, and B). I created my own constructors to ease its creation. As a result, I lose the default constructor. I dislike this, but it's easy to solve: I just make my own default constructor. Problem: My own default constructor is considered to be *initializing the variable* (even though it doesn't), whereas the original one does not. Thus, when I declare and use it before...
6
4379
by: Jai Prabhu | last post by:
Hi All, Consider the following piece of code: void func (void) { static unsigned char arr = "\x00\xAA\xBB"; fprintf (stderr, "0x%x\n", arr); fprintf (stderr, "0x%x\n", arr);
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9947
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7496
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.