Thursday, 12 September 2013

Run-Time check failure #2 - Stack around the variable 'str' was corrupted. ERROR? How to correct?

Run-Time check failure #2 - Stack around the variable 'str' was corrupted.
ERROR? How to correct?

Below program gives desired output(Counts Words of 3 consecutive lines)
but it gives "Run-Time check failure #2 - Stack around the variable 'str'
was corrupted" and hangs. I tried but I could not find the solution.
Thanks
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int i,count=0;
int main(void){
char str[3][1000];
char *ptr;
//Get user input
puts("Enter three lines:");
for (i = 0; i < 3; i++)
{
gets(&str[i][1000]);
}
for (i = 0; i < 3; i++)
{
ptr=strtok(&str[i][1000]," ");
count++;
while (ptr!=NULL)
{
ptr=strtok(NULL, " ");
if (ptr!=NULL)
{
count++;
}
}
}
printf("%d words", count);
getch();
}

No comments:

Post a Comment