Suppose I want to assign a value to a pointer.
int **a;
int** function(x){
int **b;
...
return b;
}
Is it possible?
a=function(x);
I am confused. Please let me know about it. I have checked all the lines but only the pointer assignment seems to be wrong. So, here goes the problem codes (it might be complex to see but i am simplifying it by comments, please see the comments):
int* side_x_y(int ndx, int ndy,int flag_right_0_left_1_2,int flag_up_0_down_1_2){
int * node_number=(int*)malloc(sizeof(int)*(ndx*ndy*2+1));/// ***this pointer returns to segment_matrix function***///////
for(int i=1;i<=ndx*ndy*2;i++){
node_number[i]=0;
}
//int node_number[ndx+ndy]={};
//memset ( node_number, 0, (ndx+ndy)*sizeof (int) );
if(flag_up_0_down_1_2==0){
for(int i=1;i<=ndx;i++){
node_number[i]=i;
}
}
if(flag_up_0_down_1_2==1){
for(int i=1;i<=ndx;i++){
node_number[i]=(ndy-1)*ndx+1+i;
}
}
if(flag_right_0_left_1_2==0){
for(int i=1;i<=ndy;i++){
node_number[i]=ndx*i;
}
}
if(flag_right_0_left_1_2==0){
for(int i=1;i<=ndy;i++){
node_number[i]=1+ndx*(i-1);
}
}
return node_number;
}
int** segment_matrix(int ndx,int ndy){
int *nodes;
nodes=side_x_y(ndx, ndy,1,2);////*** here i have used nodes pointer***//// it returns only 1 where it should not be one///
//for(int i=0;i<ndy;i++)
//printf("%d",nodes[i]);
int ns=ndy-1;
int **segment=(int**)malloc(sizeof(int)*(ns+1));
for(int s=1;s<=ns;s++){
segment[s]=(int*)malloc(sizeof(int)*(2+1));
}
int kk=0;
for(int s=1;s<=ns;s++){
for(int i=1;i<=2;i++){
segment[s][i]=nodes[kk];
kk++;
if(i==2){
kk--;
}
}
}
return segment;
}
INSIDE THE main function:
int **nss;
nss=segment_matrix(ndx,ndy);////*** it is the most confusing part***/// please see
//printf("%d",nss[0][0]);
int Ns=ndy-1;
double bv_T_g=0;
double bv_T_q=0;
Complex *gamma=(Complex*)malloc(sizeof(Complex)*(Ns+1));
Complex *q=(Complex*)malloc(sizeof(Complex)*(Ns+1));
for(int i=1;i<=Ns;i++){
gamma[i]=assign_complex(bv_T_g,0);
q[i]=assign_complex(bv_T_q,0);
}
int** ns=(int**)malloc(sizeof(int*)*(Ns+1));
for(int i=1;i<=Ns;i++){
ns[i]=(int*)malloc(sizeof(int)*(2+1));
}
for(int i=1;i<=Ns;i++){
for(int j=1;j<=2;j++){
ns[i][j]=0;
}
}
As written, the code is unclear, but could be OK if the value assigned to b
inside the function is safe. For example, if you allocated an array of integer pointers with calloc()
, then the code would be fine:
int **function(size_t x)
{
int **b = calloc(x * sizeof(*b));
if (b == 0)
...report error...
return b;
}
...
a = function(23);
What would not be safe is returning a pointer to a local variable, such as:
int **function(size_t x)
{
int *array[23];
int **b = array;
return b; // Undefined behaviour
}
Yes, its possible. Why are you confused?
Yes.(short & simple answer to your question)
After your update :
1st mistake is
int **segment=(int**)malloc(sizeof(int)*(ns+1));
for(int s=1;s<=ns;s++){
segment[s]=(int*)malloc(sizeof(int)*(2+1));
i think there should be
int **segment=(int**)malloc(sizeof(int*)*(ns+1));
changes
sizeof(int)--> sizeof(int*)
2> in main()
nss=segment_matrix(ndx,ndy);
you are taking output of this function in nss
which you havent use further??
3>
if(flag_right_0_left_1_2==0){
for(int i=1;i<=ndy;i++){
node_number[i]=1+ndx*(i-1);
}
you are suppose to write
if(flag_right_0_left_1_2==1){
for(int i=1;i<=ndy;i++){
node_number[i]=1+ndx*(i-1);
}
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.