1

問題は次のとおりです。ポインターからポインターを使用して、動的マトリックスを作成しましたmatrix1

このマトリックスのコピーを別のマトリックスに作成したいのですが、matrix2

私はそれをやりたいので、matrix2いじらずにいじることができるのでmatrix1 、次のことを試みました:

int main()
{
    int **matrix1, **matrix2, size1 = 10, size2 = 2;
    matrix1 = create_matrix(size1, size2);

    //I want to copy the value of matrix1 into matrixq2 and NOT the index
    **matrix2 = **matrix1
}

ただし、プログラムは中断し、次のように表示されます。エラー

見た目からして、関数を forとcreate_matrixfor の 2 回使用した方が使いやすいことがわかります。しかし、私の元のプログラムのやり方では、マトリックスを完成させるために多くのことを行うので、それはあまりにも多くの作業になるでしょう. ところで、C++ を使わないようにしたいのですが、使わずに行う方法はありますか? それは私にとってより良いでしょう。matrix1matrix2

コード「create_matrix」は次のとおりです。

//The program will read a file with the name of endereco, and create a matrix contPx3 out of it
int ** cria_matrix(char endereco[], int contP)
{
    FILE *fPointer;
    int i, contE, auxIndex, auxNum, **processos, cont_line = 0;
    char line[100];
    bool flag = true, flag2;

    fPointer = fopen(endereco, "r");

//Here the creation of the matrix
    processos = (int**)malloc(sizeof(int*) * contP);
    for (i = 0; i < contP; i++)
        processos[i] = malloc(sizeof(int) * 3);



//For now and on, is the rules of how the data will be placed on the matrix
    contP = 0;
    while (!feof(fPointer) && flag)
    {

        memset(&line[0], 'Ì', sizeof(line));
        fgets(line, 100 , fPointer);
//Bassicaly is that in each line there will be 3 numbers only, diveded but as many spaces you want. The numbeer will be placed on the matrix on the determined line they are.
        auxIndex = 0;
        flag2 = false;
        if(line[0] != '#')
            for (i = 0; i < 100; i++)
            {
                if (line[i] != ' ' && line[i] != '\n' && line[i] != '\0' && line[i] != 'Ì')//&&  line[i] != 'à'
                {
                    auxNum = line[i] - '0';
                    processos[contP][auxIndex] = auxNum;
                    auxIndex++;
                    flag2 = true;

                }
            }

        if (flag2)
            contP++;



        cont_line++;
        if (auxIndex != 3 && auxIndex != 0)
        {
            flag = false;
            printf("ERRO na linha: %d No processo: %d\nProvavelmente mais ou menos que 3 numeros separado por espacos\n", cont_line, contP);
        }

    }
    fclose(fPointer);
    if (!flag)
        system("PAUSE");
    return processos;
}
4

3 に答える 3

1

これでどうだ-

matrix2 = (int**)malloc(sizeof(int*)*size1);
for(int idx = 0; idx < size1; ++idx) {
    matrix2[idx] = (int*)malloc(sizeof(int)*size2);
    for(int idx2 = 0; idx2 < size2; ++idx2) {
        matrix2[idx][idx2] = matrix1[idx][idx2];
    }
}
于 2016-08-29T16:48:38.933 に答える
-1

ポインターのコピー、浅いコピーの作成、深いコピーの作成の違いを理解する必要があります。このことを考慮

struct employee
{
   char *name;
   float salary;
   int payrollid;
}

従業員をコピーする方法は 3 つあります

struct employee * emp1;  // points to an employee, set up somehow
struct employee * emp2;  // empty pointer, null or whatever

emp2 = emp1;  // copy pointers. emp1 and emp2 now point to the same object.

ポインタコピー

struct employee  employee1;  // employee, set up
struct employee  employee2;  // uninitalised emplyee

memcpy(&employee2, &employee1, sizeof(struct employee)); // shallow copy

浅いコピー

struct employee * emp1;  // points to an employee, set up somehow
struct employee * emp2;  // empty pointer, null or whatever

emp2 = copyemployee(emp1);

struct employee *copyemployee(struct employee *e)
{
   struct employee *answer = malloc(sizeof(struct employee));
   if(!answer)
     goto error_exit;
   answer->name = malloc(strlen(e->name) + 1);
   if(!answer->name)
      goto error_exit;
   strcpy(answer>name, e->name);
   answer->salary = e->salary;
   answer->payroolid = e->payrollid;
   return answer;
error_exit:
   /* out of memory handle somehow, usually by returning null
}

ディープコピー

ご覧のとおり、可変長フィールドが 1 つしかない単純な構造であっても、ディープ コピーを取得するのはかなりの作業です。すべてに用途がありますが、浅いコピーはおそらく 3 つの中で最も役に立たず、最もバグが発生しやすいものです。

おそらく、ポインターに割り当てる必要があるだけです。

于 2016-08-29T18:08:31.660 に答える