走同样的路,发现不同的人生
An out-of-bounds situation occurred during array access
#include <stdio.h> #include <string.h> int main() { void sort(char* []); int i; char* p[3]; char str[3][3]; for (i = 0; i < 3; ++i) { p[i] = str[i]; } printf("enter number \n"); for (i = 0; i < 3; ++i) { scanf("%s", str[i]); } sort(p); for (i = 0; i < 3; ++i) { printf("%s\n", str[i]); } return 0; } void sort(char* s[]) { printf("function called\n"); int i, j; char* t; for (i = 0; i < 3; ++i) { for (j = 0; j < 3 - i; ++j) { printf("zz\n"); //当i = 0, j < 3, j = 2时,发生数组越界, j + 1 == 3, //而str[3][3],只有3行。 if (j < 2 && strcmp(*(s + j), *(s + j + 1)) > 0) { printf("here\n"); t = *(s + j); printf("%s", t); *(s + j) = *(s + j + 1); *(s + j + 1) = t; } } } }
An out-of-bounds situation occurred during array access