473,385 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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];
}
}

Nov 15 '05 #1
3 1010
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[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];
Ditch the parens:

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


Nov 15 '05 #2
On 1 Jul 2005 22:08:27 -0700, "John" <we**********@yahoo.com> wrote:
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[i] = (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[i][j]).i = j;
(nprob.x[i][j]).d = w[i][j];
}
}


<<Remove the del for email>>
Nov 15 '05 #3


John wrote:
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[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];
}
}


Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
20
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
17
by: Christian Wittrock | last post by:
Hi, What does ANSI C say about casting an 8 bit pointer to a 16 bit one, when the byte pointer is pointing to an odd address? I have detected a problem in the Samsung CalmShine 16 compiler. This...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
9
by: Cyron | last post by:
Hello friends, Recently I have begun exploring the features that the STL map collection provides. While learning how it worked, I encountered a result that confused me. Up until now, I had...
5
by: mdh | last post by:
Hi all, I have gone through the FAQ and done searches in the Comp.Lang and if I have missed it please let me have the ref. (Question generated in part by p119). Given char a;
9
by: subramanian100in | last post by:
The following portion is from c-faq.com - comp.lang.c FAQ list · Question 6.13 int a1 = {0, 1, 2}; int a2 = {{3, 4, 5}, {6, 7, 8}}; int *ip; /* pointer to int */ int (*ap); /* pointer to...
2
by: Giorgos Keramidas | last post by:
On Sun, 05 Oct 2008 18:22:13 +0300, Giorgos Keramidas <keramida@ceid.upatras.grwrote: My apologies. I should have been less hasty to hit `post'. If showtext() is passed a null pointer, it may...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.