2

I've browsed to previously answered questions regarding pointers and matrices, but in these cases the matrices were seen as pointers to pointers. However, I am trying to create a function which read a matrix using a simple pointer and another function which prints it. This is my code, the read functions seems to work properly, but the program crashes at the printing part. If I remove the "*" from the printf statement the program works(i.e. it prints numbers from 4 to 4- I suppose this is alright, since an int is stored on 4 bytes).

void readm(int *p,int n) { p=(int *)malloc(sizeof(int)*n*n); for(int i=0;i<n*n;i++) scanf("%d",p+i); } void printm(int *p,int n) { for(int i=0;i<n;i++) { for(int j=0;j<n;j++) printf("%d ",*(p+(i*n)+j)); printf("\n"); } } 
3
  • 1
    You may be better off tagging this as C. Commented Jan 12, 2014 at 14:28
  • What do you mean the program crashes ? SEGFAULT ? Could you also include the code using those functions ? Commented Jan 12, 2014 at 14:31
  • Don't cast the return from malloc. Commented Jan 12, 2014 at 14:35

3 Answers 3

1

In the readm function you have a problem with this line:

p=(int *)malloc(sizeof(int)*n*n); 

Here you assign only to your local copy of the pointer. The variable you use when calling readm will not be changed.

You need to pass the pointer "by reference":

void readm(int **p,int n) /* Note pointer-to-pointer for `p` */ { *p=malloc(sizeof(int)*n*n); /* Note pointer-dereference of `p` */ for(int i=0;i<n*n;i++) scanf("%d",*p+i); /* Note pointer-dereference of `p` */ } 

You then have to call the function using the address-of operator:

int *p; readm(&p, X); /* Note use of address-of operator */ 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, it works properly now. Initially I thought that given the fact that I am working with a pointer the modifications which it would suffer in the function would be visible in the exterior( I associated this with the fact that when you want the modifications of a parameter to be visible outside the function you pass it using "&",hence working with its adress.)
1

A pointer to a 1D array is defined as such:

int *p1dArr; 

A pointer to a 2D array is defined as such:

int **p2dArr; 

You're using a 1D array as though it's a 2D array. This is probably the source of your troubles. Change you function definitions to the following:

void readm(int **p, int row, int col) { p = malloc(row * sizeof(*p)); for(int r = 0; r < row; r++) p[r] = malloc(col * sizeof(**p)) for(int r = 0; r < row; r++) for(int c = 0; c < col; c++) scanf("%d", &p[r][c]); } void printm(int **p, int row, int col) { for(int r = 0; r < row; r++) { for(int c = 0; c < col; c++) printf("%d ", p[r][c]); printf("\n"); } } 

Comments

1

The problem is that the calling code that calls function readm doesn't know that inside the function variable p (defined as a parameter of the function) got a new value. p is a local variable of the function and its life ends after exiting the function.

You should define the function the following way

void readm( int **p, int n ) { *p = (int *)malloc( sizeof(int ) * n * n); for ( int i=0; i<n*n; i++ ) scanf( "%d", *p+i ); } 

and call it as

int *p; readm( &p, n ); 

As for function printmthen there is no any need to redeclare it as

void printm( int **p, int n )

because it does not change the pointer. The only thing I would change is adding qualifier const

void printm( const int *p, int n ); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.