473,698 Members | 2,185 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 1591
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
1783
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
13009
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
2160
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
2109
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
3834
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
4208
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
2183
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
5837
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
1979
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
1655
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
8668
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
8598
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9152
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9014
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...
1
8885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5857
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();...
1
3037
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
2320
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.