problems with arrays

i want to declare an int array and assign a value to it. can you please help me guys.

Comments

• : i want to declare an int array and assign a value to it. can you please help me guys.
:
you have few ways to do it, one of them is as follow.
[code]
int myArr[3] = {0,0,0};
[/code]
a second way is to do as follow;
[code]
int myArr[3];
for(int index = 0;index<3;index++)
myArr[index] = 0;
[/code]
• : : i want to declare an int array and assign a value to it. can you please help me guys.
: :
: you have few ways to do it, one of them is as follow.
: [code]
: int myArr[3] = {0,0,0};
: [/code]
: a second way is to do as follow;
: [code]
: int myArr[3];
: for(int index = 0;index<3;index++)
: myArr[index] = 0;
: [/code]
:
[purple]
i think a single 0 will also do that:[code]
int array[3] = {0};
[/code]
this initializes all values to 0. will not work for other values except 0.
[/purple]

[hr][purple]~Donotalo()[/purple]

• hi there,

what i am trying to do is to read from a text file and then store each line of text in a strig array. but also i need to manipulatre the data so that i can do some calculations. for instance if my string is :

prov hiv fba orp amt
kzn 123 50 20

i need to read in the string with kzn ... and also be able to determine the numbers 123, 50 and 20 so that i can also determine the value for amt. buts thats easy, the real problem is that i need to find the total of all the values of hiv before i can determine the value for amt. e.g suppose the eqn for amt is (fba/total_hiv)* total_orp. the hiv applies to each specific row in my table. I suppose i need a string array to store the values in memory so i can then display them in a tabular format. (how do i declare a string array?) please help as i am new @ C programming.

• : [purple]
: i think a single 0 will also do that:[code]
: int array[3] = {0};
: [/code]
: this initializes all values to 0. will not work for other values except 0.
: [/purple]
:
: [hr][purple]~Donotalo()[/purple]
:
:
Ai! No way! C does not initialize variables unless you tell the compiler to do so. This is one of the major causes of bugs!
[code]
int array[3] = {0};

//initializes only array[0] to zero. If the other elements are zero too, then that's a coincedence.
[/code]

Greets,
Eric Goldstein
www.gvh-maatwerk.nl

• [b][red]This message was edited by Lundin at 2006-3-9 3:16:22[/red][/b][hr]
:
: : [purple]
: : i think a single 0 will also do that:[code]
: : int array[3] = {0};
: : [/code]
: : this initializes all values to 0. will not work for other values except 0.
: : [/purple]
: :
: : [hr][purple]~Donotalo()[/purple]
: :
: :
: Ai! No way! C does not initialize variables unless you tell the compiler to do so. This is one of the major causes of bugs!
: [code]
: int array[3] = {0};
:
: //initializes only array[0] to zero. If the other elements are zero too, then that's a coincedence.
: [/code]
:
:
: Greets,
: Eric Goldstein
: www.gvh-maatwerk.nl
:
:

He is correct, it initializes the whole array.

ISO/IEC 9899:1999 6.7.8 Initialization:
[italic]
21 If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.[/italic]

[italic]10 /--/ If an object that has static storage duration is not initialized explicitly, then: /--/

- if it has arithmetic type, it is initialized to (positive or unsigned) zero;
[/italic]

There are lots of compilers that don't follow this rule though. Make sure you use an ANSI C compiler.

• : [b][red]This message was edited by Lundin at 2006-3-9 3:16:22[/red][/b][hr]
: :
: : : [purple]
: : : i think a single 0 will also do that:[code]
: : : int array[3] = {0};
: : : [/code]
: : : this initializes all values to 0. will not work for other values except 0.
: : : [/purple]
: : :
: : : [hr][purple]~Donotalo()[/purple]
: : :
: : :
: : Ai! No way! C does not initialize variables unless you tell the compiler to do so. This is one of the major causes of bugs!
: : [code]
: : int array[3] = {0};
: :
: : //initializes only array[0] to zero. If the other elements are zero too, then that's a coincedence.
: : [/code]
: :
: :
: : Greets,
: : Eric Goldstein
: : www.gvh-maatwerk.nl
: :
: :
:
:
: He is correct, it initializes the whole array.
:
: ISO/IEC 9899:1999 6.7.8 Initialization:
: [italic]
: 21 If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.[/italic]
:
: [italic]10 /--/ If an object that has static storage duration is not initialized explicitly, then: /--/
:
: - if it has arithmetic type, it is initialized to (positive or unsigned) zero;
: [/italic]
:
:
: There are lots of compilers that don't follow this rule though. Make sure you use an ANSI C compiler.

Wow! Never knew this.
What it says is that elements of a fixed size array, globals and statics are always initialized.
However, if I declare
int a;
locally (either as class-member or local function variable, then a is not initialized.
Is that correct?

Greets,
Eric Goldstein
www.gvh-maatwerk.nl

• : : [b][red]This message was edited by Lundin at 2006-3-9 3:16:22[/red][/b][hr]
: : :
: : : : [purple]
: : : : i think a single 0 will also do that:[code]
: : : : int array[3] = {0};
: : : : [/code]
: : : : this initializes all values to 0. will not work for other values except 0.
: : : : [/purple]
: : : :
: : : : [hr][purple]~Donotalo()[/purple]
: : : :
: : : :
: : : Ai! No way! C does not initialize variables unless you tell the compiler to do so. This is one of the major causes of bugs!
: : : [code]
: : : int array[3] = {0};
: : :
: : : //initializes only array[0] to zero. If the other elements are zero too, then that's a coincedence.
: : : [/code]
: : :
: : :
: : : Greets,
: : : Eric Goldstein
: : : www.gvh-maatwerk.nl
: : :
: : :
: :
: :
: : He is correct, it initializes the whole array.
: :
: : ISO/IEC 9899:1999 6.7.8 Initialization:
: : [italic]
: : 21 If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.[/italic]
: :
: : [italic]10 /--/ If an object that has static storage duration is not initialized explicitly, then: /--/
: :
: : - if it has arithmetic type, it is initialized to (positive or unsigned) zero;
: : [/italic]
: :
: :
: : There are lots of compilers that don't follow this rule though. Make sure you use an ANSI C compiler.
:
: Wow! Never knew this.
: What it says is that elements of a fixed size array, globals and statics are always initialized.
: However, if I declare
: int a;
: locally (either as class-member or local function variable, then a is not initialized.
: Is that correct?
:
:
: Greets,
: Eric Goldstein
: www.gvh-maatwerk.nl
:

Yes. Static / global are always set to zero. Locals will contain garbage.

A local array of fixed size will only set it's contents to zero if you write something like array[5]={0}. What it means is that the first element is set to zero, and the rest of them are treated as statics, ie they are also set to zero. If you don't set any element to a value, the whole array will contain garbage.

Still, it is good programming to always set every variable to a value.
The C standard is written for RAM-based systems like a PC. If you write C for embedded systems, you probably don't want any strange code running around setting things to zero at startup, but rather to set the variable to a value before it is used for the first time. It is good to write the code in this way no matter system, sence it is safer. (This is one of the reasons why C++ allows declarations anywhere in the code, and not just in the beginning of a block.)
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!