简体   繁体   English

在指针中分配值的麻烦

[英]trouble with assigning value in pointer

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. 如所写,代码尚不清楚,但如果函数内分配给b的值是安全的,则可以确定。 For example, if you allocated an array of integer pointers with calloc() , then the code would be fine: 例如,如果您使用calloc()分配了一个整数指针数组,则代码会很好:

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() 2>在main()中

nss=segment_matrix(ndx,ndy);

you are taking output of this function in nss which you havent use further?? 您正在nss中使用此函数的输出,您还没有进一步使用?

3> 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);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM