Simple Array Question

Started by
10 comments, last by Krak 20 years, 6 months ago
Say I wanted to declare an array with 99 elements. int myArray[99]; I used to know how to, but I had a big brain fart. How would I go through and initialize each element of this using a for() loop? [edited by - Krak on October 11, 2003 7:52:32 PM]
Advertisement
ok..lets see
int myArray[99];
for(int i=0;i<99;i++)
myArray = 0;
quote:Original post by skullfire
ok..lets see
int myArray[99];
for(int k=0;k<99;k++)
myArray[k] = 0;


That works but it won't set the last item in the array it should be
 for(int k=0;k <= 99;k++)  myArray[k] = 0;    


you can also do the same like this (and its a little bit faster)
 memset(&MyArray,0,sizeof(MyArray));    



[edited by - FtMonkey on October 11, 2003 8:11:53 PM]
The monkeys are listening...
quote:Original post by FtMonkey
quote:Original post by skullfire
ok..lets see
int myArray[99];
for(int j=0;j<99;j++)
myArray[j] = 0;


That works but it won't set the last item in the array it should be
 for(int j=0;j <= 99;j++)  myArray[j] = 0;   


Actually, skullfire is correct, and his code will set the last element. Your code will access elements outside the array. The array has 99 elements, so the index ranges from 0 to 98, and you access index 99, which is outside the array, with your for loop.

edit: Changed loop index to j to prevent forum to change text to italic.

[edited by - Brother Bob on October 11, 2003 7:51:44 PM]
int myArray[99] = {0}; // initialise all elements to zero
for (int i = 0; i < 99; ++i) doNothing();
quote:Original post by skullfire
ok..lets see
int myArray[99];
for(int i=0;i<99;i++)
myArray = 0;


...And that''ll sift through every element and assign them all to zero?

quote:Original post by Brother Bob
quote:Original post by FtMonkey
quote:Original post by skullfire
ok..lets see
int myArray[99];
for(int j=0;j<99;j++)
myArray[j] = 0;


That works but it won't set the last item in the array it should be
 for(int j=0;j <= 99;j++)  myArray[j] = 0;      


Actually, skullfire is correct, and his code will set the last element. Your code will access elements outside the array. The array has 99 elements, so the index ranges from 0 to 98, and you access index 99, which is outside the array, with your for loop.

edit: Changed loop index to j to prevent forum to change text to italic.

[edited by - Brother Bob on October 11, 2003 7:51:44 PM]

Umm never mind then I guess I was the one doing it wrong hehe I forgot the last item in the array was out of range shouldn't that crash the program? I just tried it and it works as any other variable in the array...
..maybe it was just luck my program didn't crash though....



[edited by - FtMonkey on October 11, 2003 8:23:07 PM]
The monkeys are listening...
quote:Original post by FtMonkey
Umm never mind then I guess I was the one doing it wrong hehe I forgot the last item in the array was out of range shouldn''t that crash the program?

What happens when you access elements outside the array is undefined. Anything can happen, and that includes a crash.
quote:
I just tried it and it works as any other variable in the array...

The undefined behaviour you happen to get seems to work the way you want it to work. But be VERY careful, since very hard to find bugs may appear in a completely unrelated part of the program. Here''s a small example.
void bar(int *foo1, int *foo2){	*foo1 = 1;	foo2[1] = 2;	foo2[2] = 3;}int main(int, char **){	int foo1;	int foo2[2];	bar(&foo1, foo2);	cout << foo1 << endl;	return 0;} 

What do you think this code will output? 1? I get 3.
Yea I see what you mean

dev-c++: the output is 1
vc++: the output is 3
The monkeys are listening...
Brother Bob: the problem is that you index your array of two items using indices 1 and 2. You should use indices 0 and 1.

The compiler will turn:

  foo[bar] 


internally into:

  *(foo+bar) 


This is the way the language is defined (!)

If you don't believe me, check this out:

#include <stdio.h>int main(){  printf( "%d\n", 1["abcd"] );  return 0;} 


The initial solution was correct, except it was missing the indirection of
  myArray = 0;<br> </pre>  <br>(This was probably the BBS turning it into the "italic" tag)<br><br>Last: this construct will NOT initialize everything to 0, &#111;nly the first element:<br><br><pre><br>  int myArray[ 99 ] = { 0 };<br> </pre>  <br><br>However, if your variable/array is global or static, it is guaranteed to be initialized to 0 before the program starts! This is different from locals, members and heap allocations, which contain random junk initially.<br>   <br><br><SPAN CLASS=editedby>[edited by - hplus0603 on October 11, 2003 9:14:14 PM]</SPAN>
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement