pointer to pointer question

J

John

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 = (struct pair_node *)malloc(sizeof(struct
pair_node)*D);
for(j = 0; j < D; ++j){
(nprob.x[j]).i = j;
(nprob.x[j]).d = w[j];
}
}
 
M

Me

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?

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

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


Ditch the parens:

nprob.x[j].i = j;
nprob.x[j].d = w[j];
 
B

Barry Schwarz

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,?)

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?
// 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 = (struct pair_node *)malloc(sizeof(struct
pair_node)*D);


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)
for(j = 0; j < D; ++j){
(nprob.x[j]).i = j;
(nprob.x[j]).d = w[j];
}
}




<<Remove the del for email>>
 
J

John Bode

John said:
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?

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,166
Messages
2,570,901
Members
47,442
Latest member
KevinLocki

Latest Threads

Top