2

I have successfully fscanf a text file and saved in to an array E2N1. I am trying to pass this into a function as a pointer but it is not working. Whenever I try to call E2N1[0][0], it says that E2N is neither an array or a pointer. I've been looking all over for a solution on this. (Sorry E2N was meant to be E2N1) I use fscanf as:

int E2N1[noz.rowE2N][Q.N];

FILE* f = fopen("E2N.txt", "r");
for(i=0; i<noz.rowE2N; i++){
    for (j=0; j<Q.N; j++){
        fscanf(f,"%d",&E2N1[i][j]);

    }
    fscanf(f,"\n");

}
fclose(f);

and again I can't pass E2N1 into function.

Your help will be greatly appreciated.

The function is:

double *dudtF = stiffness(&U, &massM, &noz, &newV, &E2N1, &I2E, &B2E, &PP, &QQ);

and I write the function header as:

double *stiffness(double *U, double *massM, MESH *meshN, double *V1, int *E2N1, int *I2E, int *B2E, ordApprox *pp, ordApprox *qq)

V1, I2E, B2E are three arrays and I'm trying to do the same with them as I am trying to do with E2N1.

4

4 回答 4

0

您不能通过点引用来引用传递给函数的多维数组,如下所示:

int iVals[10][10];
foo(iVals);

void foo(int** pvals)
{
    // accessing the array as follows will cause an access violation
    cout << pvals[0][1]; // access violation or unpredictable results
}

您需要在函数原型中指定数组的第二维,例如:

foo(int ivals[][10])
{
    cout << ivals[0][1];  // works fine
}

如果不知道尺寸,那么我建议您遵循此处概述的原则:

void foo(int *p, int r, int c)
{
    for(int i=0; i<r; i++)
    {
        for(int j=0; j<c; j++)
        {
            printf("%d\n", p[i*c+j]);
        }
    }
}

int c[6][6];

// pointer to the first element
foo(&c[0][0], 6, 6);

// cast
foo((int*)c, 6, 6);

// dereferencing
foo(c[0], 6, 6);

// dereferencing
foo(*c, 6, 6);

我希望这有帮助。

或者,您可以使用 SAFEARRAY - 请参阅: http: //limbioliong.wordpress.com/2011/06/22/passing-multi-dimensional-managed-array-to-c-part-2/

于 2013-04-03T05:10:17.097 回答
0

Here is the example how to transfer matrix from one function to another ...

void foo (int **a_matrix)
{
int value = a_matrix[9][8];
a_matrix[9][8] = 15;
}

void main ()
{
#define ROWS 10
#define COLUMNS 10

int **matrix = 0;
matrix = new int *[ROWS] ;
for( int i = 0 ; i < ROWS ; i++ )
matrix[i] = new int[COLUMNS];

matrix[9][8] = 5;

int z = matrix[9][8] ;

foo (matrix);

z = matrix[9][8] ;
}
于 2013-04-03T04:52:11.943 回答
0

数组的有趣之处在于它们实际上充当了指针。

如果您有数组char a[3],则变量等效于char* p相同的方式,如果您有数组char b[3][4],则变量b等效于char** q. 换句话说,您应该考虑更改方法中的处理以将引用引用(并且可能再次引用)引用为整数。

试试谷歌...这是我得到的一些结果。

http://www.dailyfreecode.com/code/illustrate-2d-array-int-pointers-929.aspx

http://www.cs.cmu.edu/~ab/15-123S09/lectures/Lecture%2006%20-%20%20Pointer%20to%20a%20pointer.pdf

于 2013-04-03T04:42:25.520 回答
0

您不需要传递 as &E2N1,只需传递 as E2N1no &,因为数组名称本身会转换为指针。

double *dudtF = stiffness(&U, &massM, &noz, &newV, E2N1, &I2E, &B2E, &PP, &QQ);

此外,您需要将其int **视为其二维数组。

double *stiffness(double *U, double *massM, MESH *meshN, double *V1, int **E2N1, int *I2E, int *B2E, ordApprox *pp, ordApprox *qq)
于 2013-04-03T04:43:02.043 回答