Hi, Everyone!
Does anyone know how to initialize an int array with a non-zero number?
Thank You Very Much.
Truly Yours, Simon Dexter 13 27163
<si******@yahoo .com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.com Hi, Everyone!
Does anyone know how to initialize an int array with a non-zero number?
Thank You Very Much.
Truly Yours, Simon Dexter
Int is not int. C++ is case sensitive.
The only way to do this at the point of declaration is to repeat the number
as many times as you need it, e.g.,
int array[5] = {9,9,9,9,9};
Alternatively, you can do it in a loop after the declaration.
Vectors allow you to avoid repeating the number:
std::vector<int > array(5, 9);
--
John Carson
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
-- OR --
int intArr[n1]; // declaration
intArr[0] = x1; // initialization
It all depends on how far you want to go. You can give it a completer
list in the first example if you want. Or, you can use a for loop
that runs through the array index until it has filled the last
element, using the second statement inside the loop, substituting your
index variable for the zero and applying whatever number you
application feels is appropriate for the array. Thank You Very Much.
You're welcome.
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."
"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.
--
John Carson
>> 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.
if we write int a[20]={ 1,2};
then it will initialize only first two elements, and leave all others
uninitialized.
Still if you want to initialize an array with 0's, you have to write it
expilicitly.
int a[3]={0,0,0}; or use std::vector, std::vector<int > a(size,0);
The answer is whether u want to initialize array with zero or non-zero
value , you have to explicitly initialize them.
> Does anyone know how to initialize an int array with a non-zero number?
did anyone mention memset?
like, int * pi = new int (123);
memset(pi, <some byte here>, 123*sizeof(int) );
pity you'd have pretty limited initial values set to choose from.
On Wed, 13 Jul 2005 11:44:15 +0400, upashu2 <up*****@rediff mail.com> 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. if we write int a[20]={ 1,2}; then it will initialize only first two elements, and leave all others uninitialized.
This is wrong.
[dcl.init.aggr] 8.5.1 Aggregates
....
7 If there are fewer initializers in the list than there are members in
the aggregate, then each member not explicitly initialized shall be
value-initialized (8.5).
[Example:
struct S { int a; char* b; int c; };
S ss = { 1, "asdf" };
initializes ss.a with 1, ss.b with "asdf", and ss.c with the value of an
expression of the form int(), that is, 0. ]
--
Maxim Yegorushkin
<fi************ ****@gmail.com>
On Wed, 13 Jul 2005 08:46:19 +0400, <si******@yahoo .com> wrote: Does anyone know how to initialize an int array with a non-zero number?
Just iterate over the array and initialize its members with any values you
like.
The standard library provides some basic function templates for filling
arrays as well as ranges:
fill/fill_n
generate
Example:
int a[10];
fill_n(a, sizeof(a) / sizeof(*a), 1); // fill with ones
--
Maxim Yegorushkin
<fi************ ****@gmail.com>
<ma************ @gmail.com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com Does anyone know how to initialize an int array with a non-zero number? did anyone mention memset?
like, int * pi = new int (123);
That should be
int *pi = new int[123];
memset(pi, <some byte here>, 123*sizeof(int) );
This does byte by byte initialisation, which is viable for the very small
fraction of integers which have the same number in each byte.
pity you'd have pretty limited initial values set to choose from.
Small and obsure. You cannot do this:
memset(pi, 5, 123*sizeof(int) );
and initialise all integers to 5. You initialise all integers to whatever
number consists of a 5 in each byte.
--
John Carson
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. com 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. Why are you centering on
one item and not the whole post?
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." This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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;
|
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()
{
|
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;
|
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 );
};
|
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....
| |
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;
|
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?
|
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?
|
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...
|
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);
|
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,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |