0

I don t get any errors at compilation. The program just crashes when I run it. I tried to print the matrix directly from the generate function and it printed the first line and a bit of the second.

This is my code

void generate(int **a)//*Function to generate a matrix a[i][j]=i+j*
{
    int i,j;
    for(i=0;i<5;i++){
        for(j=0;j<4;j++){
            a[i][j]=i+j;
        }
    }
}

void print(int **a)//*print the resulting matrix from generate function*
{
    int i,j;
    for(i=0;i<5;i++){
        for(j=0;j<4;j++){
            printf("%d ",a[i][j]);
        }
        printf("\n");
    }
}

int main()
{
    int *a=(int*)malloc(5*4*sizeof(int));//*allocating memory for a matrix of 4 lines and 5 columns.*
    generate(&a);
    print(&a);
}
DYZ
  • 55,249
  • 10
  • 64
  • 93
Mamaliga
  • 3
  • 1
  • 1
    Your functions have no idea what rows and columns there are. They do not receive a 2-star pointer just because you want them to. Check compiler warnings. – Weather Vane Jan 29 '17 at 20:39
  • 1
    A pointer to a pointer (`&a`) is not the same as a two-dimensional array. – DYZ Jan 29 '17 at 20:44

2 Answers2

1

1) you are allocating a single dimension memory.

a[i][j]=i+j; //is not valid.

Below is the modified code

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

void generate(int *a)//*Function to generate a matrix a[i][j]=i+j*
{
    int i,j;
    for(i=0;i<5;i++){
        for(j=0;j<4;j++){
            *a=i+j;
                a++; //increments to next memory location
        }
    }
}

void print(int *a)//*print the resulting matrix from generate function*
{
    int i,j;
    for(i=0;i<5;i++){
        for(j=0;j<4;j++){
            printf("%d ",*(a++)); //notice another way of accessing 
        }
        printf("\n");
    }
}

int main()
{
    int *a=(int*)malloc(5*4*sizeof(int));//*allocating memory for a matrix of 4 lines and 5 columns.*
    generate(a);
    print(a); //passing the pointer
    free(a); //Always always practice to free the allocated memory, don't ever do this mistake again
    return 0;
}
Dilip Kumar
  • 1,736
  • 11
  • 22
0

Two things:

First, int ** denotes a pointer to a pointer that points to an int, not a pointer that points to an array of ints.

Second, when you just pass a pointer to some data structure, e.g. an 4x5 array of integers, then the compiler cannot derive the layout of this data structure. I.e. a statement like a[i][j] would require that the compiler "knows" that each row i consists of 4 columns j, such that it can calculate the "place" to which the value should be stored, i.e. a + (4*i) + j. The compiler simply does not know the nr of columns per row, i.e. 4.

To overcome this while keeping the size of the array at least potentially variable (note that "4" and "5" are still hard coded in the function), you could do the following:

void generate(int *a)//*Function to generate a matrix a[i][j]=i+j*
{
    int i,j;
    for(i=0;i<5;i++){
        for(j=0;j<4;j++){
            *(a+(i*4+j)) = i+j;
        }
    }
}

void print(int *a)//*print the resulting matrix from generate function*
{
    int i,j;
    for(i=0;i<5;i++){
        for(j=0;j<4;j++){
            printf("%d ", *(a+(i*4+j)));
        }
        printf("\n");
    }
}

int main()
{
    int *a=(int*)malloc(5*4*sizeof(int));//*allocating memory for a matrix of 4 lines and 5 columns.*
    generate(a);
    print(a);
}
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58