itializing arrays

Started by
7 comments, last by DrewSmug 19 years, 11 months ago
i have an array of mine initialized like so... for(int c; c<20; c++) { for(int i; i<10; i++) { itsArrayB[c] = 0; } } i get an access violation when i do this. itsArrayB is a private array thing in my class and i am trying to initialize it in the constructor. is that bad? because it works right if i initialize it in any other function within my class. what gives, does anybody know why my vc++ compiler does this.
Advertisement
First of all, are you sure that your array has enough elements? Secondly, what is the point of the inner loop? All it does is initialize the array to the same value 20 times.

edit: just noticed, the loop should read as this:

for(int c=0; c<20; c++){  for(int i=0; i<10; i++)  {    itsArrayB[c] = 0;  }}


Note that the loop counters start at 0, otherwise they are assigned a random number and will go out of bounds of your array.

[edited by - eleusive on May 6, 2004 7:51:57 PM]
"What are you trying to tell me? That I can write an O(N^2) recursive solution for a 2-dimensional knapsack?" "No, programmer. I'm trying to tell you that when you're ready, you won't have to." -Adapted from "The Matrix"
You didn''t set either counter to zero. for (int i = 0; i < 20; i++)
first off sorry for the typo(it happens)
i meant
itsArrayB[c][i]


so that way there are enough elements

but Deyja was right, i just needed to initialize my lame counters. i would have guessed that my compiler would have cought something like that though. but yea, thanks for your help

[edit] the forum is what actually made my syntax look wrong so i stuck it in those source tag things.

[edited by - drewsmug on May 6, 2004 8:13:12 PM]
What''s the declaration of itsArrayB?

My guess is that it''s not declared as itsArrayB[20][10].
enum Bool { True, False, FileNotFound };
naw i declared that right.
but i have a new array initialization question.
here is my new array

	for(c; c<20; c++);	{		itsArrayY[c] = ((c * 20) + 4);	}


i thought this would initialize the array so that...
itsArrayY[0] = 4;
itsArrayY[1] = 24;
itsArrayY[2] = 44;
and so on continually incrementing itself by 20

but it doesn''t do that. does anybody know what''s wrong with my math.
Again, what''s the initial value of c?

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
sorry,
but like always i had c previously declared to start at 0.
Tell us what it is doing, and please include ALL pertinent code in your source tags. This includes ALL declarations previously made, and all assignments previously made. Include the declarations of your arrays and other variables and their assignments and it is a lot easier to offer real help, and not ask questions that shouldn''t have to be asked.

This topic is closed to new replies.

Advertisement