在C语言中,访问数组会受到两种限制:
原因:
解决方法:
strncpy
而不是strcpy
来复制字符串,以防止缓冲区溢出。示例代码:
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int index = 10;
if (index >= 0 && index < 5) {
printf("Element at index %d is %d\n", index, arr[index]);
} else {
printf("Index out of bounds\n");
}
return 0;
}
参考链接:
领取专属 10元无门槛券
手把手带您无忧上云