如何在不知道C中确切的行数的情况下将多维数组复制到另一个数组?

时间:2020-09-04 14:06:05

标签: arrays c multidimensional-array

因此,我的程序基本上是为了获取游戏得分,跟踪w / l / t等而构建的。我需要做的是根据对手的得分以升序排列得分。 为此,我决定进行冒泡排序,但是当我进行冒泡排序并打印出来时,第一对[0] [0]和[0] [1]产生了很大的负数,这就是我的猜测是他们的参考,然后其余部分可以正确打印。我已经四处搜寻,却找不到任何相关信息,因此我想将原始数组复制到副本中,然后尝试对该数组进行排序。

#include <stdio.h>
#include <stdlib.h>
#define _CRT_SECURE_NO_WARNINGS //doesn't work
#define FLUSH myFlush()
#define GAMES 50

void titleDisplay(int);
void menuOptions();
void gameResults(int *counter, int team[][2], int); // records games from user input
void showRecord(int counter, int team[][2]); // shows current record
void displayResultFromGamesWon(int counter, int team[][2]);
void displayAllResults(int counter, int team[][2]); // shows all results ordered by opp score.
char getChoice();
void myFlush();

int main() {
    //const int GAMES = 50; - program doesn't read this as a const when used to create arr.
    const int MAX_GAMES = 50;
    int userTeam[GAMES][2]; // array column 0 is  user score, column 1 is opp score
    int gameCounter = 0;
    char userChoice;

    do {
        system("clear");
        titleDisplay(1);
        menuOptions();
        userChoice = getChoice();
        switch (userChoice) {
        case 'a': case 'A':
            gameResults(&gameCounter, userTeam, MAX_GAMES);
            break;
        case 'b': case 'B':
            showRecord(gameCounter, userTeam);
            break;
        case 'c': case 'C':
            displayResultFromGamesWon(gameCounter, userTeam);
            break;
        case 'd': case 'D':
            displayAllResults(gameCounter, userTeam);
            break;
        case 'e': case 'E':
            printf("Bye bye.\n");
            system("pause");
            break;
        default:
            printf("Invalid selection, choose again!\n");
            system("pause");
            break;
        }//end switch
    } while (userChoice != 'e' && userChoice != 'E');
    return 0;
}

在这里我进行排序和打印:

//function definition
void displayAllResults(int counter, int team[][2]) {
    int i;
    int temp, temp2 = 0;
    system("clear");
    if (counter == 0) {
        printf("\n\n\tYou haven't played any games yet.\n\n\n");
    }
    else {
        titleDisplay(4);
        printf("\t  (Arranged by Opponent score low to high)\n\n");
        printf("\tUser Score\t\t\tOpponent Score\n");
        printf("\t----------\t\t\t--------------\n");
        //begin bubble sorting 
        for (int x = 0; x < counter; x++) {
            for (int y = x + 1; y < counter; y++) {
                if (team[x][0] > team[y][0]) {
                    temp = team[x][1];
                    temp2 = team[x][0];
                    team[x][0] = team[y][0];
                    team[x][1] = team[y][1];
                    team[y][0] = temp2;
                    team[y][1] = temp;
                }//end if
            }
        }//end bubble sort
        for (i = 0; i < counter; i++) {
            printf("\t%-8i\t\t\t%-11i\n", team[i][0], team[i][1]);
        }//end for
    }//end else
    system("pause");
}//end function

我尝试声明一个变量'int sortedArray = team [counter] [2];'在displayAllResults函数中,但这会导致内存问题,并且在我尝试访问该变量时失败。我尝试了memcpy,但要么执行不正确,要么不起作用。

甚至可以将这样的2D数组复制到另一个数组吗?

1 个答案:

答案 0 :(得分:-1)

有两种将数组复制到另一个数组的方法: 1-使用sizeof(array)并将其除以数组一个元素的大小,在这种情况下,它是一个指针。 2-最好的方法是拥有一个变量,该变量将向您显示数组的长度。

我认为您犯了一个实施错误。我不明白你在做什么。 5有什么用? 无论如何,请为数组的长度设置一个变量。

相关问题