Public Group

#### Archived

This topic is now archived and is closed to further replies.

# Simple Array Question

This topic is 5460 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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]

##### Share on other sites
ok..lets see
int myArray[99];
for(int i=0;i<99;i++)
myArray = 0;

##### Share on other sites
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]

##### Share on other sites
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]

##### Share on other sites
int myArray[99] = {0}; // initialise all elements to zero
for (int i = 0; i < 99; ++i) doNothing();

##### Share on other sites
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?

##### Share on other sites
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]

##### Share on other sites
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.

##### Share on other sites
Yea I see what you mean

dev-c++: the output is 1
vc++: the output is 3

##### Share on other sites
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[ i] = 0;

(This was probably the BBS turning it into the "italic" tag)

Last: this construct will NOT initialize everything to 0, only the first element:

  int myArray[ 99 ] = { 0 };

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.

[edited by - hplus0603 on October 11, 2003 9:14:14 PM]

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
12
5. 5

• 14
• 9
• 22
• 9
• 31
• ### Forum Statistics

• Total Topics
632618
• Total Posts
3007485
• ### Who's Online (See full list)

There are no registered users currently online

×