search

Home  >  Q&A  >  body text

c++ - 求大神帮忙解答 

怪我咯怪我咯2773 days ago341

reply all(1)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-17 14:35:23

    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;
                }
            }
        }
    }

    reply
    0
  • Cancelreply