0

I have a C procedure that is supposed to use a for loop to initialize each element in an array to zero. However, on the first pass, I keep getting this error: Exception thrown at 0x00D25538 in Homework6.exe: 0xC0000005: Access violation writing location 0x036FE514. I've stepped through the code with the debugger, and I haven't seen any obvious problems (then again, as a beginner, I'm not sure I know what to look for). Is there something wrong with my code that could be causing the issue?

Here is my code:

long InitializeArray(long lngMyArray) 
{
    for (int intIndex = 0; intIndex < lngARRAY_SIZE; intIndex += 1)
    {
        lngMyArray[&intIndex] = 0; // THIS LINE THROWS THE ERROR
    }

    return lngMyArray;
}

Thanks for any help, I really appreciate it!

Ethan Malloy
  • 555
  • 1
  • 4
  • 16
  • 1
    `long` is not an array type. This is not an array access. Using the address of an index to access something that is not an array is even more bewilderingly strange, and further from your goal. Do you mean `long*`? C allows for some really stupid things to compile, like `2[i]` being the same as `i[2]`. – tadman Mar 06 '21 at 01:07
  • 1
    This function, to stick to common C conventions, should look like `void InitializeArray(long* array, size_t size)` where you pass *both* the array and its associated size. Having `lngARRAY_SIZE` as a global is just messy and error-prone. Reminder that all this code boils down to, effectively, a call to `bzero()` or `memset()` so it doesn't even need to exist. – tadman Mar 06 '21 at 01:09
  • [Here's a list of books to help you get started](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). – user3386109 Mar 06 '21 at 01:10

1 Answers1

0

I solved the problem. This was the solution:

long InitializeArray(long *lngMyArray) 
{
    for (int intIndex = 0; intIndex < lngARRAY_SIZE; intIndex += 1)
    {
        lngMyArray[intIndex] = 0;
    }

    return lngMyArray;
}
Ethan Malloy
  • 555
  • 1
  • 4
  • 16