#include<stdio.h>
#define ARRAY_LEN (3)
typedef struct _INT_ARRAY {
int m_data[ARRAY_LEN];
} INT_ARRAY;
// set
// 0 : ok
// 1 : param error
// 2 : index error
int INT_ARRAY__Set(INT_ARRAY * p, int index, int d)
{
if (NULL == p) {
return 1;
}
if (index < 0 || ARRAY_LEN <= index) {
return 2;
}
p->m_data[index] = d;
return 0;
}
// get
// 0 : ok
// 1 : param error
// 2 : index error
int INT_ARRAY__Get(INT_ARRAY * p, int index, int *d)
{
if (NULL == p || NULL == d) {
return 1;
}
if (index < 0 || ARRAY_LEN <= index) {
return 2;
}
*d = p->m_data[index];
return 0;
}
int main()
{
INT_ARRAY int_data;
int i, res, d;
for (i = 0; i < ARRAY_LEN; i++) {
res = INT_ARRAY__Set(&int_data, i, i);
if (res) {
return 1;
}
}
for (i = -1; i < ARRAY_LEN + 1; i++) { // 範囲外アクセス
printf("int_data.m_data[%2d] = ", i
); res = INT_ARRAY__Get(&int_data, i, &d);
if (res) {
} else {
}
}
return 0;
}
I2luY2x1ZGU8c3RkaW8uaD4KCiNkZWZpbmUgQVJSQVlfTEVOICAoMykKCnR5cGVkZWYgc3RydWN0IF9JTlRfQVJSQVkgewogICAgaW50IG1fZGF0YVtBUlJBWV9MRU5dOwp9IElOVF9BUlJBWTsKCi8vIHNldAovLyAgMCA6IG9rCi8vICAxIDogcGFyYW0gZXJyb3IKLy8gIDIgOiBpbmRleCBlcnJvcgppbnQgSU5UX0FSUkFZX19TZXQoSU5UX0FSUkFZICogcCwgaW50IGluZGV4LCBpbnQgZCkKewogICAgaWYgKE5VTEwgPT0gcCkgewogICAgICAgIHJldHVybiAxOwogICAgfQogICAgaWYgKGluZGV4IDwgMCB8fCBBUlJBWV9MRU4gPD0gaW5kZXgpIHsKICAgICAgICByZXR1cm4gMjsKICAgIH0KICAgIHAtPm1fZGF0YVtpbmRleF0gPSBkOwogICAgcmV0dXJuIDA7Cn0KCi8vIGdldAovLyAgMCA6IG9rCi8vICAxIDogcGFyYW0gZXJyb3IKLy8gIDIgOiBpbmRleCBlcnJvcgppbnQgSU5UX0FSUkFZX19HZXQoSU5UX0FSUkFZICogcCwgaW50IGluZGV4LCBpbnQgKmQpCnsKICAgIGlmIChOVUxMID09IHAgfHwgTlVMTCA9PSBkKSB7CiAgICAgICAgcmV0dXJuIDE7CiAgICB9CiAgICBpZiAoaW5kZXggPCAwIHx8IEFSUkFZX0xFTiA8PSBpbmRleCkgewogICAgICAgIHJldHVybiAyOwogICAgfQogICAgKmQgPSBwLT5tX2RhdGFbaW5kZXhdOwogICAgcmV0dXJuIDA7Cn0KCmludCBtYWluKCkKewogICAgSU5UX0FSUkFZIGludF9kYXRhOwogICAgaW50IGksIHJlcywgZDsKCiAgICBmb3IgKGkgPSAwOyBpIDwgQVJSQVlfTEVOOyBpKyspIHsKICAgICAgICByZXMgPSBJTlRfQVJSQVlfX1NldCgmaW50X2RhdGEsIGksIGkpOwogICAgICAgIGlmIChyZXMpIHsKICAgICAgICAgICAgcHJpbnRmKCJlcnJvclxuIik7CiAgICAgICAgICAgIHJldHVybiAxOwogICAgICAgIH0KICAgIH0KICAgIGZvciAoaSA9IC0xOyBpIDwgQVJSQVlfTEVOICsgMTsgaSsrKSB7IC8vIOevhOWbsuWkluOCouOCr+OCu+OCuQogICAgICAgIHByaW50ZigiaW50X2RhdGEubV9kYXRhWyUyZF0gPSAiLCBpKTsKICAgICAgICByZXMgPSBJTlRfQVJSQVlfX0dldCgmaW50X2RhdGEsIGksICZkKTsKICAgICAgICBpZiAocmVzKSB7CiAgICAgICAgICAgIHByaW50ZigiZXJyb3JcbiIpOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHByaW50ZigiJWRcbiIsIGQpOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiAwOwp9Cg==