Question:
Passing an array to external function error! C programming?
dudemeister88
2009-11-15 04:45:30 UTC
Ok, so I have written some code and as it is 400 lines long I can't include it all, I have just included from where the error probably occurs.

.......in main function ..................
for(i=750;i<800;i++)
printf("\ndatain[%d] = %lf",i,datain[i]);
myfft(datain,dataout,nn,direc);

.........

void myfft(double datain[],double dataout[],unsigned long nn,int direc){

unsigned long n, mmax, m, j, istep, i;
double normalise[3], wtemp, wr, wpr, wpi, wi, theta, tempr, tempi;
normalise[2] = 1.0;
normalise[0] = 1.0/(double)nn;
n = nn << 1;
j = 1;
mmax = 2;

for(i=750;i<800;i++)
printf("\ndatain[%d] = %lf",i,datain[i]);

but when I do this the two printf output different values of datain[i]!! I can't see how it could go wrong in between here as I haven't changed datain[i] in anyway apart from passing it to the external function and printing the values. If anyone could help that would be great and if you need any more information let me know. Thanks!!

Also, the above question is what I really want solved but a quick question, if i pass datain-1 to the external function does this just take the count of datain[i] to datain[i-1].

Cheers for the help!!!!!!!!!
Five answers:
?
2009-11-15 05:34:46 UTC
i think it is difficult to know the prob. without full code

but make sure that u have make all the element of array initially zero..



and u have to pass an array by its reference like '&datain' to the function it works better
Bill
2009-11-16 12:18:57 UTC
You are passing the array incorrectly. What you have done here is attempt to pass an array by value. It isn't going to happen in C/C++. They must be passed by reference. In this version a new address space is allocated within the function stack then you print from that point forward. So you are getting junk values the second time around.



place the symbol & in front of the value from where you try to pass it to a function. This will instead pass the memory address location of that variable to the function where that function will then directly modify the values (global changes)
?
2009-11-16 05:24:57 UTC
Put all the code on a site like pastebin.com and then post a link to it here.
?
2016-10-17 13:47:58 UTC
in case you're passing an array, then bypass the pointer and the indices you pick to apply. #contain #contain #contain glide MinorDeterminant(glide mat[3][3], int i, int j) { glide minordet; if (i==0 && j==0) minordet = (mat[a million][a million] * mat[2][2]) - (mat[2][a million] * mat[a million][2]); if (i==0 && j==a million) minordet = (mat[a million][0] * mat[2][2]) - (mat[2][0] * mat[a million][2]); if (i==0 && j==2) minordet = (mat[a million][0] * mat[2][a million]) - (mat[2][0] * mat[a million][a million]); return(minordet); } int substantial(int argc, char* argv[]) { document *enter; int i, j; glide matrix[3][3], determinant; const char inp_fn[]="matrix.dat"; /* Open archives */ enter = fopen(inp_fn, "r"); if( (enter != (document*) NULL) ) { { printf("| "); for (i = 0; i < 3; i++) for (j = 0; j < 3; j++) { fscanf(enter, "%f", &matrix[i][j]); printf("%.4f ", matrix[i][j]); if (j == 2 && i < 2) printf("| n |"); } printf("|n"); } for(i=0, j=0; j<=2; j++) { determinant += (glide)(pow(-a million, j)) * (matrix[i][j]) * MinorDeterminant(matrix, i, j); /*errors: incompatible type for argument a million of 'MinorDeterminant' notice: predicted 'glide (*)[3]' yet argument is of vogue 'glide'/*/ printf("Determinant = %f", determinant); } fclose(enter); return(0); } else { printf("*** would desire to no longer open enter document, or there are no longer 9 aspects interior the matrix ***n"); go out(EXIT_FAILURE); } }
S1989
2009-11-15 04:55:39 UTC
check your initial values.....can't solve without full code...give some link where we can see the code.....

OR give us the problem statement....


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...