When running another piece of my new program, the debugging is okay but after that it reported a "Access violation writing location...".
Using the breakpoint function, I find that the following function has something wrong:
void initialize (int temp[MAXLINE], int digit, int nd)
{
int i;
for(i=0; i<=MAXLINE; ++i)
{
temp[i]=0;
}
digit=nd=0;
}
In fact, the error report appears right after the sentence "temp[i]=0;".
Could anybody help me with it? I know that access violation means that the data address is not correct but I just cannot figure out what's the problem...
Here i post all of the code in this program... I hope someone could tell me what's wrong with it...
#include <stdio.h>
#include <stdlib.h>
#define MAXLINE 10000
void printarray (int array[], int digitnumber);
void initialize (int array[], int digit, int nd);
main()
{
int digit, temp[MAXLINE];
int nd,i ;
nd=digit=0;
for(i=0; i<MAXLINE; ++i)
{
temp[i]=0;
}
while((nd=getchar())!=EOF)
{
if(nd!='\n')
{
temp[digit]=nd;
++digit;
}
else if (digit>=80)
{
printarray(temp[MAXLINE], digit);
initialize(temp[MAXLINE], digit, nd);
}
else initialize(temp[MAXLINE], digit, nd);
}
return 0;
}
void printarray (int array[MAXLINE], int digitnumber)
{
int i;
for(i=0; digitnumber>0; --digitnumber)
{
putchar(array[i]);
++i;
}
printf("\n");
}
void initialize (int array[MAXLINE], int digit, int nd)
{
for(digit; digit>0; --digit)
{
array[digit]=0;
}
digit=nd=0;
}
I also noticed that the K&R says:
"When the name of an array is used as an argument, the value passed to the function is the location or address of the beginning of the array-- there is no copying of array elements."
Does my mistake has anything to do with the use of array?...Thanks for your help!