Menu

#45 referencing Array element

open
nobody
None
5
2007-07-02
2007-07-02
RaceMouse
No

Hi,

Please consider this piece of code:
-------------------------------------------
#include <stdio.h>
#include <string.h>

static void func1 (int a)
{
int array[10] = {0,1,2,3,4,5,6,7,8,9};

printf("char is %i\n",array[a]);
array[a]++;
}

int main (void)
{
int i;

for (i=0;i < 15; i++)
{
func1 (i);
}
return i;
}
-------------------------------------------
This causes a segmentation fault because there is no checking of array[a]. When 'a' is more than (sizeof(array)-1) we seg-fault. Is it possible to check for this ?

<string.h> has been included but no function in the header file has been referenced. Is it possible to check for this ?

Cheers
RaceMouse

Discussion


Log in to post a comment.

MongoDB Logo MongoDB