473,715 Members | 5,414 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pointer to pointers of structs.

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[i].n = i + P * 2;
obstacles[i].v = malloc(sizeof obstacles[i].v * obstacles[i].n);

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

Feb 19 '06 #1
12 1595
Ico
jo********@gmai l.com wrote:
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' ?

--
:wq
^X^Cy^K^X^C^C^C ^C
Feb 19 '06 #2
>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.

Feb 19 '06 #3
jo********@gmai l.com wrote:
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[i].n = i + P * 2;
obstacles[i].v = malloc(sizeof obstacles[i].v * obstacles[i].n);

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

Feb 19 '06 #4
In article <11************ **********@g47g 2000cwa.googleg roups.com>
<jo********@gma il.com> wrote:
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[i].n = i + P * 2;
obstacles[i].v = malloc(sizeof obstacles[i].v * obstacles[i].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? :-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Feb 19 '06 #5
<jo********@gma il.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .
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[i].n = i + P * 2;
obstacles[i].v = malloc(sizeof obstacles[i].v * obstacles[i].n);
You mean: sizeof *obstacles[i].v
for(j = 0; j < obstacles[i].n; i++)
You mean: j++
{
obstacles[i].v[j].x = obstacles[i].n;
obstacles[i].v[j].y = obstacles[i].n + 5;
}
}
Maybe you should consider returning 0 here. }


Feb 19 '06 #6
jo********@gmai l.com wrote:
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?
<snip> #define P 2

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

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

for(i = 0; i < P; i++)
{ <snip> for(j = 0; j < obstacles[i].n; i++)

_______________ _______________ _______________ __^^^__

Don't you mean j++

--
If you're posting through Google read <http://cfaj.freeshell. org/google>
Feb 19 '06 #7
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.

Feb 19 '06 #8
In article <11************ **********@o13g 2000cwo.googleg roups.com>
<jo********@gma il.com> wrote:
... 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[i].member[j].member2 = val;

Any time you see a[b], 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)).membe r, 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. :-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Feb 19 '06 #9
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.

Feb 19 '06 #10

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

Similar topics

1
1786
by: mrhicks | last post by:
Hello all, I need some advice/help on a particular problem I am having. I have a basic struct called "indv_rpt_rply" that holds information for a particular device in our system which I will call INDV. The struct looks like // Some info used for the struct typedef unsigned char uint8; /* 8 bits */
204
13038
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 = {0,1,2,4,9};
48
2164
by: yezi | last post by:
Hi, all: I want to record some memory pointer returned from malloc, is possible the code like below? int memo_index; int i,j; char *tmp; for (i=0;i<10;i++){
24
2111
by: venkatesh | last post by:
hai, any can tell what is the advantge of using pointers?any increase in speed or execution time? if you know please tell what are the real time applications of data structure? thanks in advance
15
3836
by: Paminu | last post by:
Still having a few problems with malloc and pointers. I have made a struct. Now I would like to make a pointer an array with 4 pointers to this struct. #include <stdlib.h> #include <stdio.h> typedef struct _tnode_t { void *content; struct _tnode_t *kids;
51
4214
by: atv | last post by:
Hi, Just to check, if i set a pointer explicitly to NULL, i'm not allowed to dereference it? Why is that, it's not like it's pointing to any garbage right? Why else set it to NULL. I can remember some instances where i did just that and printf reported something like : (null). I'm asking because i read somewhere that it is not valid. I thought it simply wasn't valid to dereference a pointer who is _not_ set to point to anything.
7
2184
by: Ray Dillinger | last post by:
Hi. I'm having a problem and I really want to understand it. Here's the situation: I have an array of pointers, and each pointer is the head of a linked list of structs. The structs are typedef'd to have the type name 'event.' It's a simple open hash table used to implement events in a game. So, I have a routine that returns the address of one of the pointers in the array, or returns NULL for a key out of range (meaning no bucket for...
156
5856
by: Lame Duck | last post by:
Hi Group! I have a vector<floatvariable that I need to pass to a function, but the function takes a float * arguement. That's OK, I can convert by doing &MyVector.front(), but when I get back a float * from the function, how to convert that back to a vector? Thanks in advance!
14
1981
by: Szabolcs Borsanyi | last post by:
Deal all, The type typedef double ***tmp_tensor3; is meant to represent a three-dimensional array. For some reasons the standard array-of-array-of-array will not work in my case. Can I convert an object of this type to the following type?
0
1659
by: mjaaland | last post by:
Hi! I've been working with DLLimports passing structs and various other parameters to unmanaged code. I had problems earlier sending pointer to structs to the unmanaged code, and this forum solved it for me (using the ref keyword etc). I now encountered a function that takes a pointer to an array as a parameter, and this array consists of other pointers, to structs. Horrible isn't it? I have no way of modifying the unmanaged code, I...
0
8823
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9198
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9047
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7973
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5967
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4477
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4738
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3175
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2541
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.