Pointer to pointers of structs.

J

johnsolver

Hi I hope I got the topic right..

that's what I'm doing:
create P(variable) polygons each having a variable numbers of vertices,
something in my mallocs is wrong... since I get weird results.

help?

#include <stdio.h>
#include <stdlib.h>

typedef struct point
{
double x, y;
} point;

typedef struct polygon
{
int n;
point *v;
} polygon;

typedef struct line
{
point p1, p2;
double A, B;
} line;

#define P 2

int main()
{
int i = 0, j = 0;

polygon *obstacles = malloc(sizeof *obstacles * P);

for(i = 0; i < P; i++)
{
obstacles.n = i + P * 2;
obstacles.v = malloc(sizeof obstacles.v * obstacles.n);

for(j = 0; j < obstacles.n; i++)
{
obstacles.v[j].x = obstacles.n;
obstacles.v[j].y = obstacles.n + 5;
}
}
}
 
I

Ico

create P(variable) polygons each having a variable numbers of vertices,
something in my mallocs is wrong... since I get weird results.

Please be more specific: what do you mean by 'weird results' ?
 
J

johnsolver

Please be more specific: what do you mean by 'weird results' ?
To make it clearer, the program doesn't finish runing. I'm quite
certain I've made a copmlete mess out of the mem-allocation...

Thanks.
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Hi I hope I got the topic right..

that's what I'm doing:
create P(variable) polygons each having a variable numbers of vertices,
something in my mallocs is wrong... since I get weird results.

help? [...]


int main()
{
int i = 0, j = 0;

polygon *obstacles = malloc(sizeof *obstacles * P);

for(i = 0; i < P; i++)
{
obstacles.n = i + P * 2;
obstacles.v = malloc(sizeof obstacles.v * obstacles.n);

for(j = 0; j < obstacles.n; i++)

^^
Likely you mean j++ in the above for loop.
{
obstacles.v[j].x = obstacles.n;
obstacles.v[j].y = obstacles.n + 5;
}
}
}
 
C

Chris Torek

polygon *obstacles = malloc(sizeof *obstacles * P);

This line is good (although I would put the numeric multiplier
first, i.e.:

polygon *obstacles = malloc(P * sizeof *obstacles);

as I just find this more readable).
for(i = 0; i < P; i++)
{
obstacles.n = i + P * 2;
obstacles.v = malloc(sizeof obstacles.v * obstacles.n);


This malloc() line is ... not good. A malloc() call should read:

var = malloc(number * sizeof *var);

or (with your multiplication order);

var = malloc(sizeof *var * number);

The last line quoted above is missing a character. Is it obvious
yet which one? :)
 
S

stathis gotsis

Hi I hope I got the topic right..

that's what I'm doing:
create P(variable) polygons each having a variable numbers of vertices,
something in my mallocs is wrong... since I get weird results.

help?

#include <stdio.h>
#include <stdlib.h>

typedef struct point
{
double x, y;
} point;

typedef struct polygon
{
int n;
point *v;
} polygon;

typedef struct line
{
point p1, p2;
double A, B;
} line;

#define P 2

int main()
{
int i = 0, j = 0;

polygon *obstacles = malloc(sizeof *obstacles * P);

for(i = 0; i < P; i++)
{
obstacles.n = i + P * 2;
obstacles.v = malloc(sizeof obstacles.v * obstacles.n);


You mean: sizeof *obstacles.v
for(j = 0; j < obstacles.n; i++)


You mean: j++
{
obstacles.v[j].x = obstacles.n;
obstacles.v[j].y = obstacles.n + 5;
}
}


Maybe you should consider returning 0 here.
 
P

Pedro Graca

that's what I'm doing:
create P(variable) polygons each having a variable numbers of vertices,
something in my mallocs is wrong... since I get weird results.

help?

#define P 2

int main()
{
int i = 0, j = 0;

polygon *obstacles = malloc(sizeof *obstacles * P);

for(i = 0; i < P; i++)
{
for(j = 0; j < obstacles.n; i++)

_______________________________________________^^^__

Don't you mean j++
 
J

johnsolver

Bugger, I deserve this, mixing up i's and j's ... and forgetting the *,
many thanks. One more point on the same subject, I was wandering how do
I rewrite the above code using the (->) operator instead of the indices
[k] method I used?

Theanks.
 
C

Chris Torek

... One more point on the same subject, I was wandering how do
I rewrite the above code using the (->) operator instead of the indices
[k] method I used?

Well, the code is no longer "above", but an example line in question
was something like:

p.member[j].member2 = val;

Any time you see a, you can rewrite it as (*((a) + (b))) -- and
often several of the parentheses are unneeded -- so this could be
written:

(*((*(p + i)).member + j)).member2 = val;

Any time you see (*(expr)).member, you can rewrite it as (expr)->member,
i.e., remove the asterisk and replace the dot with "->" -- and again
you can remove various unneeded parentheses -- so the above can be
re-rewritten as:

(((p + i))->member + j)->member2 = val;

The intermediate form is *much* worse than the original array syntax,
and the final form is *still* worse than the original array syntax --
so stick with the array syntax. :)

Note, incidentally, that since p->memb is equvalent to (*p).memb,
and *p is equivalent to p[0], p->memb can always be rewritten
as p[0].memb, too. This is mainly useful for obfuscating code. :)
 
J

johnsolver

Chris Torek wrote:
so the above can be
re-rewritten as:

(((p + i))->member + j)->member2 = val;

The intermediate form is *much* worse than the original array syntax,
and the final form is *still* worse than the original array syntax --
so stick with the array syntax. :)
Yeah I've stumbled upon it when trying to rewrite, the thing is that
I've seen in many places advices on the use of the -> operator as being
'easier' and clearer way, seems like this isn't the case.

Many thanks.
 
K

Keith Thompson

To make it clearer, the program doesn't finish runing. I'm quite
certain I've made a copmlete mess out of the mem-allocation...

I think your question has already been answered elsethread, but I'll
just mention that "the program doesn't finish running" isn't a very
helpful description. Anyone trying to help you is going to be far
less interested in what your program *doesn't* do than in what it
*does* do. In other words, you need to specify exactly *how* it
"doesn't finish running", what output it produces, whether it crashes
or hangs, etc.
 
R

Richard G. Riley

Chris Torek wrote:
so the above can be
Yeah I've stumbled upon it when trying to rewrite, the thing is that
I've seen in many places advices on the use of the -> operator as being
'easier' and clearer way, seems like this isn't the case.

Many thanks.

Try them both and maybe use intermediate variables. personally I much
prefer something like (off top of head and not compilable)

char * pChar = malloc(n*sizeof(char));
for(char *p=pChar,int i=0;i<n;i++)
*p++=getChar();

to the array syntax.

It can often be horses for courses : but familarity with pointer
notation is a definite plus.

As the previous poster said though, it can be messy if
you dont use intermediate pointers.
 
A

August Karlstrom

Bugger, I deserve this, mixing up i's and j's ... and forgetting the *,
many thanks.

I usually use k as index in single loops and j and k in doubly nested
loops. Much more distinct than i and/or j.


August
 

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
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top