Okay here is one of the questions that i wanted to figure out. C does not have any array bounds checking as in Java and this can many a times lead to buffer overrun or the program space getting corrupted. I have this program
[insert]
Code:
#include <stdio.h>
#include <stdlib.h>
struct name
{
char arr[20];
int age;
};
int main(void)
{
struct name n;
struct name *np;
struct name **npp;
np = &n;
npp = &np;
printf("\n Enter your name.");
scanf("%s", n.arr);
printf("\n Enter your age.");
//scanf("%d", &np->age);
scanf("%d", &n.age);
//printf("\n Your name is %s", np->arr);
//printf("\n Your age is %d", np->age);
printf("\n Your name is %s", (*npp)->arr);
printf("\n Your age is %d", (*npp)->age);
return 0;
}
Now if i enter more than 20 chars in the arr what does it do? Does it overwrite the memory adjacent to those 20 spaces that the arr has in its possession. Also if i do so at the end of the program completion i get the message
"Stack aroudn the variable n was corrupted"
So is this an indication of the fact that i have done a buffer overrun. Also how do i implement bound checking so that these mistakes can be avoided.