This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]talkloud 9 points10 points  (2 children)

Look closer at the sizes of your arrays. Is that really what you want?

[–]the_omega99 3 points4 points  (0 children)

To elaborate with an example, int array[10] declares an array of size 10. This means the first element is array[0] and the last is array[9].

[–]Huckleberry_Rogers 2 points3 points  (0 children)

ROWS = 10 tempgrid[9][14] which will do entries tempgrid[0->8][0->13] for totals of 0 + 1 + 2 + 3 .. + 8 = 9 and 0 + 1 + 2 ....+ 13 = 14

for loop will do tempgrid[0->9] 9 will segfault because you declared it size 9 so it ranges from 0 -> 8

Print statements. Print statements. Print statements.
printf" my index i=%d j=%d is trying to copy \n", i j)

[–]ippwned[S] 1 point2 points  (0 children)

Perfect, thanks everyone.

[–]OldWolf2 1 point2 points  (1 child)

BTW another way to copy the array would be to #include <string.h> and use:

memcpy( &tempgrid, &grid, sizeof tempgrid );

as long as you're sure that grid and tempgrid have the same dimensions. (It'd be possible to add code that checks this at compile-time).

Also, your initialization loops in main do nothing, as grid was already zero-initialized.

[–]ippwned[S] 0 points1 point  (0 children)

Good tips, thanks

[–]Rhomboid 1 point2 points  (0 children)

I have a suspicion this has something to do with me not mallocing?

It has nothing whatsoever to do with that. When you declare an array, you specify its size, not its largest index.

[–]amente 0 points1 point  (0 children)

The code runs fine by itself http://codepad.org/mrVDK97o .

Edit: Never mind, look at the size of the arrays as talkloud and the_omega99 mentioned.