Problem in passing an array to a function - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Problem in passing an array to a function

loke137loke137 Posts: 8Member
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
#include

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("
");
}

printf("
");

return 0;
}


And the function:

#include <stdio.h>
#include




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

Comments

  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    : The main() file
    :
    : #include
    : #include
    :
    : void load (char *data, [color=Red]float (*matrix)[2][/color]);
    :
    : main()
    : {
    :
    : char data[] = "data.txt";
    : FILE *input;
    : [color=Red]float matrix[2][2];[/color]
    : int i,j;
    :
    :
    : load(data, matrix);
    :
    :
    : for (i = 0; i<=1; i++)
    : {
    : for(j = 0; j<=1; j++)
    : printf("%f ",matrix[j][i]);
    : printf("
    ");
    : }
    :
    : printf("
    ");
    :
    : return 0;
    : }
    :
    :
    : And the function:
    :
    : #include <stdio.h>
    : #include
    :
    :
    :
    :
    : void load(char *data, [color=Red]float (*matrix)[2][/color])
    :
    : {
    :
    : 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
  • loke137loke137 Posts: 8Member
    : : The main() file
    : :
    : : #include
    : : #include
    : :
    : : void load (char *data, [color=Red]float (*matrix)[2][/color]);
    : :
    : : main()
    : : {
    : :
    : : char data[] = "data.txt";
    : : FILE *input;
    : : [color=Red]float matrix[2][2];[/color]
    : : int i,j;
    : :
    : :
    : : load(data, matrix);
    : :
    : :
    : : for (i = 0; i<=1; i++)
    : : {
    : : for(j = 0; j<=1; j++)
    : : printf("%f ",matrix[j][i]);
    : : printf("
    ");
    : : }
    : :
    : : printf("
    ");
    : :
    : : return 0;
    : : }
    : :
    : :
    : : And the function:
    : :
    : : #include <stdio.h>
    : : #include
    : :
    : :
    : :
    : :
    : : void load(char *data, [color=Red]float (*matrix)[2][/color])
    : :
    : : {
    : :
    : : 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??
  • LundinLundin Posts: 3,711Member
    : Thanks. I will try that.
    : You mean the coding indentation??
    :


    Code tags ftw:

    [code]
    int main()
    {
    printf("Hello world");
    return 0;
    }
    [code]
Sign In or Register to comment.