0

this code should create an array in main and then print it but every time I run it I just get an array of all 0s

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

void print(float **A, int w, int h){
  int i, j;

  A = (float*) malloc(w*h*sizeof(float));

  for (i=0;i<h;i++){
    A[i] = (float*) malloc(w*sizeof(float));
  }

  for (i=0; i<h; i++){
    for(j=0; j<w; j++){
      printf("%f ", A[i][j]);
    }

    printf("\n");
  }
}

int main(void) {

    int i;
    int x_dimension=3;
    int y_dimension=2;
    float arr [3][2]={};
    arr[0][0]=16.2;

    print(arr,x_dimension,y_dimension);

    return 0;
}
2
  • Why bother allocating memory in print()? After all, you are just printing its content. Commented Mar 5, 2014 at 0:52
  • 1
    float[M][N] and float** are not the same. Commented Mar 5, 2014 at 0:56

2 Answers 2

0

You're re-allocing it in your print function, this should work:

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

void print(float * A,int h, int w){
  int i,j;
  for (i=0;i<h;i++){
    for(j=0;j<w;j++){
      printf("%f ",A[i * w + j]);
    }
    printf("\n");
  }
}

int main(void) {
    int i;
    const int x_dimension=3;
    const int y_dimension=2;
    float arr[x_dimension][y_dimension];
    arr[0][0]=16.2;
    print(arr,x_dimension,y_dimension);
    return 0;
}

Note that I also inverted the w and h parameters in the print function.

Sign up to request clarification or add additional context in comments.

4 Comments

Why are you bending the rules, instead of doing the correct solution and fix the function parameters.
@self. what do you mean?
@jlhonora could you explain how your printf statement works, and was there any reason you changed the w and h around?
0

I think you see the arr is not initialized:

  1. alloc memory?
  2. default values.
  3. float arr[][] is not same with float **arr, you should use like this: float (*A)[2]

But in your main function, you have finished the alloc work. The arr is allocated at the stack. So in your print function, what you have to do is only print the result or initialize each item's value.

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

void printArr(float (*A)[2],int w, int h){
    int i,j;
    //Wrong, A has been alloced.
    /*
       A = (float*) malloc(w*h*sizeof(float));
       for (i=0;i<h;i++){
       A[i]=(float*) malloc(w*sizeof(float));
       }
       */
    for (i=0;i<h;i++){
        for(j=0;j<w;j++){
            printf("%f ",A[i][j]);
        }
        printf("\n");
    }
}

void printPointer(float *A,int w, int h){
    int i,j;
    //Wrong, A has been alloced.
    /*
       A = (float*) malloc(w*h*sizeof(float));
       for (i=0;i<h;i++){
       A[i]=(float*) malloc(w*sizeof(float));
       }
       */
    for (i=0;i<h;i++){
        for(j=0;j<w;j++){
            printf("%f ",*((A+i*h)+j));
        }
        printf("\n");
    }
}
int main(void) {
    int x_dimension=3;
    int y_dimension=2;

    //By Array, Array is not a pointer, but is a structure
    float arr[2][3] = {};
    //Only [0][0] item has been initialized
    arr[0][0]=16.2f;
    printArr(arr,x_dimension,y_dimension);

    //By pointer
    float* arrp = (float*)calloc(x_dimension*y_dimension,sizeof(float));
    *arrp=16.2f;
    printPointer(arrp,x_dimension,y_dimension);


    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.