I have a function I would like to have a function to receive a 2D array (H), to read a specified column (col) and to pass it to another array (b). It appears that the code below is not OK has I was expecting to get a 1 printed. Any guidance is very much appreciated.
#define nline 5
#define ncol 4
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
void count0(int **M,int col, int *a);
void main(){
int i;
int **H;
H=(int**)malloc(nline*sizeof(int*));
for(i=0;i<nline;i++){
H[i]=(int*)malloc(ncol*sizeof(int));
}
H[0][0]=8;
H[0][1]=5;
H[0][2]=6;
H[0][3]=0;
H[1][0]=7;
H[1][1]=5;
H[1][2]=4;
H[1][3]=0;
H[2][0]=5;
H[2][1]=1;
H[2][2]=1;
H[2][3]=7;
H[3][0]=0;
H[3][1]=0;
H[3][2]=0;
H[3][3]=2;
H[4][0]=1;
H[4][1]=0;
H[4][2]=1;
H[4][3]=4;
int *b;
int col=1;
count0(H,col,&b);
printf("num 0=%d\n",b[2]); getchar();
}
/////////////////////////////////////////////////////////////////////////////////
void count0(int **M,int col, int *a){
int i;
a=(int*)malloc(1*sizeof(int));
for(i=0;i<nline;i++){
a[i]=M[i][col];
a=realloc(a,(i+2)*sizeof(int));
}
}
Your argument for the output b
is wrong. It needs to be a pointer to a pointer (like how you pass it). Right now you should get a compiler warning about it. The function prototype should be
void count0(int **M,int col, int **a);
Then in the function you need to use the dereference operator to access a
:
*a = malloc(1*sizeof(int));
Note that I don't cast the return value of malloc
, there no need to and neither should you.
As you already knows the number of rows = nline
in your matrix in count0 function. So you should simply allocate all memory for array a
in count function one time, you don't need to re-call realloc() function.
void count0(int **M, int col, int** a){
(*a) = malloc(nline * sizeof(int));
for(i = 0; i < nline; i++){
(*a)[i] = M[i][col];
}
}
Note: precedence of []
is higher then *
so you need ()
around *a
just call this function as: count0(H, col, &b);
and free(b);
in main() after printf statement to deallocate memory explicitly.
You are trying to have this variable:
int * b;
Point to an newly allocated array after this call:
count0(H,col,&b);
To do this, you must change the signature of count 0 to the following:
void count0(int **M,int col, int **a);
Note that int *a is now int **a. Then in the body of count0, you must use the following lines to allocate/reallocate a:
*a=(int*)malloc(1*sizeof(int));
*a=realloc(a,(i+2)*sizeof(int));
Right now, a
is a pointer variable that only exists in the scope of count0
. Initially its value is &b
(which right now doesn't make sense since &b
is an int**
, and a
is an int*
). You have a
point to newly allocated memory, which is lost after the function. The changes I suggest will have *a
, and therefore b
, to point to memory that can be retrieved after the function call.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.