pointer to pointer question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John

    #1

    pointer to pointer question

    struct pair_node
    {
    int i;
    double d;
    };

    struct pair_node **x;
    The following is what I want to create.

    x -> [ ] -> (2,0.1) (3,0.2) (-1,?)
    [ ] -> (2,0.1) (3,0.3) (4,-1.2) (-1,?)
    [ ] -> (1,0.4) (-1,?)
    [ ] -> (2,0.1) (4,1.4) (5,0.5) (-1,?)
    [ ] -> (1,-0.1) (2,-0.2) (3,0.1) (4,1.1) (5,0.1) (-1,?)


    // Do I need an allocation before this loop for x?

    for(i = 0; i < N; ++i){
    //dumps core on the following line, anyone can help me why?
    // am i missing something?
    nprob.x[i] = (struct pair_node *)malloc(sizeof (struct
    pair_node)*D);
    for(j = 0; j < D; ++j){
    (nprob.x[i][j]).i = j;
    (nprob.x[i][j]).d = w[i][j];
    }
    }

  • Me

    #2
    Re: pointer to pointer question

    > struct pair_node[color=blue]
    > {
    > int i;
    > double d;
    > };
    >
    > struct pair_node **x;
    > The following is what I want to create.
    >
    > x -> [ ] -> (2,0.1) (3,0.2) (-1,?)
    > [ ] -> (2,0.1) (3,0.3) (4,-1.2) (-1,?)
    > [ ] -> (1,0.4) (-1,?)
    > [ ] -> (2,0.1) (4,1.4) (5,0.5) (-1,?)
    > [ ] -> (1,-0.1) (2,-0.2) (3,0.1) (4,1.1) (5,0.1) (-1,?)
    >
    >
    > // Do I need an allocation before this loop for x?[/color]

    Yeah. Or I guess nprob.x by the looks of it:

    nprob.x = (struct pair_node**)mal loc(sizeof(stru ct pair_node*)*N);
    [color=blue]
    > for(i = 0; i < N; ++i){
    > //dumps core on the following line, anyone can help me why?
    > // am i missing something?
    > nprob.x[i] = (struct pair_node *)malloc(sizeof (struct
    > pair_node)*D);
    > for(j = 0; j < D; ++j){
    > (nprob.x[i][j]).i = j;
    > (nprob.x[i][j]).d = w[i][j];[/color]

    Ditch the parens:

    nprob.x[i][j].i = j;
    nprob.x[i][j].d = w[i][j];
    [color=blue]
    > }
    > }[/color]

    Comment

    • Barry Schwarz

      #3
      Re: pointer to pointer question

      On 1 Jul 2005 22:08:27 -0700, "John" <weekender_ny@y ahoo.com> wrote:
      [color=blue]
      >struct pair_node
      > {
      > int i;
      > double d;
      > };
      >
      >struct pair_node **x;
      >The following is what I want to create.
      >
      > x -> [ ] -> (2,0.1) (3,0.2) (-1,?)
      > [ ] -> (2,0.1) (3,0.3) (4,-1.2) (-1,?)
      > [ ] -> (1,0.4) (-1,?)
      > [ ] -> (2,0.1) (4,1.4) (5,0.5) (-1,?)
      > [ ] -> (1,-0.1) (2,-0.2) (3,0.1) (4,1.1) (5,0.1) (-1,?)[/color]

      Is it really your intent to have a "ragged matrix" where row 0 has 3
      elements, row 1 has 4, row 2 has 2, etc?
      [color=blue]
      >
      >
      >// Do I need an allocation before this loop for x?
      >
      > for(i = 0; i < N; ++i){
      > //dumps core on the following line, anyone can help me why?
      > // am i missing something?
      > nprob.x[i] = (struct pair_node *)malloc(sizeof (struct
      >pair_node)*D );[/color]

      Or will every row hold D elements?

      Don't cast the return from malloc. It only serves to prevent the
      compiler from warning you about a particular type of undefined
      behavior.

      What is nprob? Perchance is nprod.x your struct **? If so, then it
      must be initialized to point to an area of memory suitable for holding
      some number (apparently N) of struct *. You could use

      nprob.x = malloc(N * sizeof *nprob.x)
      [color=blue]
      > for(j = 0; j < D; ++j){
      > (nprob.x[i][j]).i = j;
      > (nprob.x[i][j]).d = w[i][j];
      > }
      > }[/color]



      <<Remove the del for email>>

      Comment

      • John Bode

        #4
        Re: pointer to pointer question



        John wrote:[color=blue]
        > struct pair_node
        > {
        > int i;
        > double d;
        > };
        >
        > struct pair_node **x;
        > The following is what I want to create.
        >
        > x -> [ ] -> (2,0.1) (3,0.2) (-1,?)
        > [ ] -> (2,0.1) (3,0.3) (4,-1.2) (-1,?)
        > [ ] -> (1,0.4) (-1,?)
        > [ ] -> (2,0.1) (4,1.4) (5,0.5) (-1,?)
        > [ ] -> (1,-0.1) (2,-0.2) (3,0.1) (4,1.1) (5,0.1) (-1,?)
        >
        >
        > // Do I need an allocation before this loop for x?
        >[/color]

        Yes. Given the above example:

        struct pair_node **x;
        x = malloc(sizeof *x * 5);
        if (x)
        {
        x[0] = malloc(sizeof **x * 3);
        x[1] = malloc(sizeof **x * 4);
        x[2] = malloc(sizeof **x * 2);
        x[3] = malloc(sizeof **x * 4);
        x[4] = malloc(sizeof **x * 6);
        }
        [color=blue]
        > for(i = 0; i < N; ++i){
        > //dumps core on the following line, anyone can help me why?
        > // am i missing something?
        > nprob.x[i] = (struct pair_node *)malloc(sizeof (struct
        > pair_node)*D);
        > for(j = 0; j < D; ++j){
        > (nprob.x[i][j]).i = j;
        > (nprob.x[i][j]).d = w[i][j];
        > }
        > }[/color]

        Comment

        Working...