View Single Post
Old 09-13-2014, 09:03 AM   #10
Not Evil, Just British
hyperme's Avatar
Join Date: Mar 2008
Location: Britain
Posts: 1,322
Default Re: Problem: Functions In C

Firstly: C, like most languages, uses zero indexing. I have no idea how memory works in C, but in C++ or Java starting from 1 is a really neat way to waste memory.

Secondly: Your for loops are completely wrong. Unless C treats all attempts to access invalid array indexes as -1, they aren't going to work*. At all. Instead, find out the length of your array, put in a variable then have something like this:

for(int i = 0; i <= arrayLength; i++)
//Do stuff
I'm fairly certain this will fix your problem.

*Assuming arrays work in C and C++ the same, they won't work because arrays are bunch of data store concurrently in memory. Data is accesses based on the array type by using an offset from the memory address of element 0 of the array. This means C doesn't care that you're out of the arrays bounds. Your loop will continue until it hits a location storing a -1. Any other data in between will be process by the function as an int. Since you're roaming all over the memory map, it's likely that you'll hit an empty memory location, which is read as 0. (In Java, your program would exception. For several reasons.)
This space unintentionally left blank
hyperme is offline   Reply With Quote