有人可以告诉我为什么chain1正在打印99吗?

我想做的就是在条件完成后将chain1和chain3交换,但是chain1突然跳到99,我真的不知道为什么。我什至更改了其他字符串,效果很好。
在这种情况下,键入a3,然后键入c3,将chain1与chain3交换。

#include <stdio.h>
#include <stdlib.h>
#define R 3
#define C 3
#include <time.h>
#define S 100

int main()
{
    char CHAINASK[S];

    int index = 0;
    int z = 0;
    int Z = 0;

    char CHAIN1[S];
    char CHAIN2[S];
    char CHAIN3[S];
    char CHAIN4[S];
    char CHAIN5[S];
    char CHAIN6[S];
    char CHAIN7[S];
    char CHAIN8[S];
    char CHAIN9[S];

    int a, b;

    int c, n;

    ///random maker
    srand(time(NULL));
    for (c = 1; c <= 1; c++)
    {
        n = rand() % 4 + 1;
        printf("%d\n", n);
    }

    printf("\n");
    printf("\n");
    CHAIN1[S] = rand() % 4 + 1;
    CHAIN2[S] = rand() % 4 + 1;
    CHAIN3[S] = rand() % 4 + 1;
    CHAIN4[S] = rand() % 4 + 1;
    CHAIN5[S] = rand() % 4 + 1;
    CHAIN6[S] = rand() % 4 + 1;
    CHAIN7[S] = rand() % 4 + 1;
    CHAIN8[S] = rand() % 4 + 1;
    CHAIN9[S] = rand() % 4 + 1;

    int q[R][C] = {CHAIN1[S], CHAIN2[S], CHAIN3[S], CHAIN4[S], CHAIN5[S], CHAIN6[S], CHAIN7[S], CHAIN8[S], CHAIN9[S]};

    for (a = 0; a < R; a++)
    {
        for (b = 0; b < C; b++)
        {
        }
    }

    // PRINTS THE TABLE
    for (a = 0; a < R; a++)
    {
        printf("\n");
        for (b = 0; b < C; b++)
        {
            printf("%d,", q[a][b]);
        }
    }
    printf("\n");
    printf("\n");

////////////////////////////////
// the comparer
////////////////////////////////

    printf("Escriba su coordenada\n");
    scanf("%s", CHAINASK);
    printf("\n");
    printf("\n");

    if (strcmp(CHAINASK, "a3") == 0)
    {
        printf("eligio la casilla a3");
        printf("\n");
        printf("\n");
        printf("Escriba su coordenada\n");
        scanf("%s", CHAINASK);
        printf("\n");
        printf("\n");
        if (strcmp(CHAINASK, "c3") == 0)
        {
            printf("eligio la casilla c3");
            printf("\n");
            printf("\n");

            // THE SWAP OCCURS

            int q[R][C] = {CHAIN1[S], CHAIN2[S], CHAIN3[S], CHAIN4[S], CHAIN5[S], CHAIN6[S], CHAIN9[S] CHAIN7[S], CHAIN8[S], };

            for (a = 0; a < R; a++)
            {
                for (b = 0; b < C; b++)
                {
                }
            }
            // PRINTS THE TABLE
            for (a = 0; a < R; a++)
            {
                printf("\n");
                for (b = 0; b < C; b++)
                {
                    printf("%d,", q[a][b]);
                }
            }
        }
    }

    if (strcmp(CHAINASK, "b3") == 0)
    {
        printf("eligio la casilla b3");
    }
    else
    {
        printf("no valid coordinate was introduced");
    }
}

最佳答案

问题是您正在访问位置S = 100或CHAIN1 [100]中的CHAINx,
您对CHAINx的声明是char CHAIN1 [S]或CHAIN1 [100],这意味着在读取或写入CHAINx时,您的索引不得高于99,例如CHAIN [99]

一种解决方法是将您的CHAIN声明为char CHAIN [S + 1],此处为固定代码。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>


#define R 3
#define C 3
#define S 100
int main()
{
char CHAINASK[S+1];

int index = 0;
int z = 0;
int Z = 0;

char CHAIN1[S+1];
char CHAIN2[S+1];
char CHAIN3[S+1];
char CHAIN4[S+1];
char CHAIN5[S+1];
char CHAIN6[S+1];
char CHAIN7[S+1];
char CHAIN8[S+1];
char CHAIN9[S+1];

int a, b;

int c, n;

///random maker
srand(time(NULL));
for (c = 1; c <= 1; c++)
{
    n = rand() % 4 + 1;
    printf("%d\n", n);
}

printf("\n");
printf("\n");
CHAIN1[S] = rand() % 4 + 1;
CHAIN2[S] = rand() % 4 + 1;
CHAIN3[S] = rand() % 4 + 1;
CHAIN4[S] = rand() % 4 + 1;
CHAIN5[S] = rand() % 4 + 1;
CHAIN6[S] = rand() % 4 + 1;
CHAIN7[S] = rand() % 4 + 1;
CHAIN8[S] = rand() % 4 + 1;
CHAIN9[S] = rand() % 4 + 1;

int q[R][C] = {CHAIN1[S], CHAIN2[S], CHAIN3[S], CHAIN4[S], CHAIN5[S], CHAIN6[S], CHAIN7[S], CHAIN8[S], CHAIN9[S]};

for (a = 0; a < R; a++)
{
    for (b = 0; b < C; b++)
    {
    }
}

// PRINTS THE TABLE
for (a = 0; a < R; a++)
{
    printf("\n");
    for (b = 0; b < C; b++)
    {
        printf(" %d[%d,%d] , ", q[a][b],a,b);
    }
}
printf("\n");
printf("\n");

// the comparer


printf("Escriba su coordenada\n");
scanf("%s", CHAINASK);
printf("\n");
printf("\n");

if (strcmp(CHAINASK, "a3") == 0)
{
    printf("eligio la casilla a3");
    printf("\n");
    printf("\n");
    printf("Escriba su coordenada\n");
    scanf("%s", CHAINASK);
    printf("\n");
    printf("\n");

    if (strcmp(CHAINASK, "c3") == 0)
    {
        printf("eligio la casilla c3");
        printf("\n");
        printf("\n");

        // THE SWAP OCCURS

        int qd[R][C] = {CHAIN1[S], CHAIN2[S], CHAIN3[S],  CHAIN4[S], CHAIN5[S], CHAIN6[S],CHAIN9[S], CHAIN7[S], CHAIN8[S], };

        for (a = 0; a < R; a++)
        {
            for (b = 0; b < C; b++)
            {
            }
        }
        // PRINTS THE TABLE
        for (a = 0; a < R; a++)
        {
            printf("\n");
            for (b = 0; b < C; b++)
            {
                printf(" %d [%d,%d] , ", qd[a][b],a,b);
            }
        }
    }
}

if (strcmp(CHAINASK, "b3") == 0)
{
    printf("eligio la casilla b3");
}
else
{
    printf("no valid coordinate was introduced");
}
}

09-26 01:08