Skip to content

Instantly share code, notes, and snippets.

@rk76feWF
Last active March 13, 2023 07:05
Show Gist options
  • Save rk76feWF/0b54ed292044bd6834e95c2a38e9e460 to your computer and use it in GitHub Desktop.
Save rk76feWF/0b54ed292044bd6834e95c2a38e9e460 to your computer and use it in GitHub Desktop.
[C言語] 配列の範囲外アクセスを抑制する
#include <stdio.h>
enum
{
TABLESIZE = 10
};
static int table[TABLESIZE] = {12, 14, 16, 77, 65};
int *f(size_t index)
{
if (index < TABLESIZE)
return table + index;
return NULL;
}
int main(void)
{
for (int i = -3; i < 5; ++i)
{
int *num = f(i);
if (num == NULL)
{
printf("Error");
continue;
}
printf("%d ", *num);
}
return 0;
}
@rk76feWF
Copy link
Author

continue忘れてた。
thank you!
@kAwA1290

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment