Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5432
Number of posts: 16953

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Problem in passing an array to a function Posted by loke137 on 31 May 2007 at 9:47 AM
Hello,

I am trying to pass a float array from a funtion. I wrote an test program to get the idea first. At first, I tried loading numbers from a file, but some were returned wrong. So I tried assigning numbers to the matrix (as in the following code) and still one came out wrong (matrix[0][1]). Does someone know where is my mistake?
Thanks


The main() file

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

void load (char *data, float (*matrix)[1]);

main()
{

char data[] = "data.txt";
FILE *input;
float matrix[1][1];
int i,j;


load(data, matrix);


for (i = 0; i<=1; i++)
{
for(j = 0; j<=1; j++)
printf("%f ",matrix[j][i]);
printf("\n");
}

printf("\n");

return 0;
}


And the function:

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




void load(char *data, float (*matrix)[1])

{

FILE *input;



matrix[0][0] = 1.00;
matrix[0][1] = 4.2;
matrix[1][0] = 10.28;
matrix[1][1] = 9.87;


return;

}

I supposed the output should be

1.000000 4.200000
10.280000 9.870000

But it actually is:
1.000000 10.280000
10.280000 9.870000
Report
Re: Problem in passing an array to a function Posted by BitByBit_Thor on 31 May 2007 at 12:15 PM
: The main() file
:
: #include <stdio.h>
: #include <stdlib.h>
:
: void load (char *data, float (*matrix)[2]);
:
: main()
: {
:
: char data[] = "data.txt";
: FILE *input;
: float matrix[2][2];
: int i,j;
:
:
: load(data, matrix);
:
:
: for (i = 0; i<=1; i++)
: {
: for(j = 0; j<=1; j++)
: printf("%f ",matrix[j][i]);
: printf("\n");
: }
:
: printf("\n");
:
: return 0;
: }
:
:
: And the function:
:
: #include <stdio.h>
: #include <stdlib.h>
:
:
:
:
: void load(char *data, float (*matrix)[2])
:
: {
:
: FILE *input;
:
:
:
: matrix[0][0] = 1.00;
: matrix[0][1] = 4.2;
: matrix[1][0] = 10.28;
: matrix[1][1] = 9.87;
:
:
: return;
:
: }
:

I made some changes in red. Basically, you were telling C that your array is 1x1, while you wanted it to be 2x2.


Best Regards,
Richard

The way I see it... Well, it's all pretty blurry
Report
Re: Problem in passing an array to a function Posted by loke137 on 1 Jun 2007 at 4:30 AM
: : The main() file
: :
: : #include <stdio.h>
: : #include <stdlib.h>
: :
: : void load (char *data, float (*matrix)[2]);
: :
: : main()
: : {
: :
: : char data[] = "data.txt";
: : FILE *input;
: : float matrix[2][2];
: : int i,j;
: :
: :
: : load(data, matrix);
: :
: :
: : for (i = 0; i<=1; i++)
: : {
: : for(j = 0; j<=1; j++)
: : printf("%f ",matrix[j][i]);
: : printf("\n");
: : }
: :
: : printf("\n");
: :
: : return 0;
: : }
: :
: :
: : And the function:
: :
: : #include <stdio.h>
: : #include <stdlib.h>
: :
: :
: :
: :
: : void load(char *data, float (*matrix)[2])
: :
: : {
: :
: : FILE *input;
: :
: :
: :
: : matrix[0][0] = 1.00;
: : matrix[0][1] = 4.2;
: : matrix[1][0] = 10.28;
: : matrix[1][1] = 9.87;
: :
: :
: : return;
: :
: : }
: :
:
: I made some changes in red. Basically, you were telling C that your
: array is 1x1, while you wanted it to be 2x2.
:
:
: Best Regards,
: Richard
:
: The way I see it... Well, it's all pretty blurry

Thanks. I will try that.
You mean the coding indentation??
Report
Re: Problem in passing an array to a function Posted by Lundin on 1 Jun 2007 at 6:04 AM
: Thanks. I will try that.
: You mean the coding indentation??
:


Code tags ftw:

int main()
{
  printf("Hello world");
  return 0;
}



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.