c++ - 求大神帮忙解答 

【字号: 日期:2023-04-18浏览:28作者:雯心

问题描述

问题解答

回答1:

数组访问发生越界情况

#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('%sn', str[i]); } return 0;}void sort(char* s[]){ printf('function calledn'); int i, j; char* t; for (i = 0; i < 3; ++i) {for (j = 0; j < 3 - i; ++j){ printf('zzn'); //当i = 0, j < 3, j = 2时,发生数组越界, j + 1 == 3, //而str[3][3],只有3行。 if (j < 2 && strcmp(*(s + j), *(s + j + 1)) > 0) {printf('heren');t = *(s + j);printf('%s', t);*(s + j) = *(s + j + 1);*(s + j + 1) = t; }} }}

相关文章: