R
Roman Hartmann
hello,
I do have a question regarding structs. I have a struct (profil) which
has a pointer to another struct (point). The struct profil stores the
coordinates of points.
The problem is that I don't know how many points
there will be in every struct in the end, so I have to allocate memory
dynamically for
them and can't use an array of fixed size, unfortunately.
I would like to know if there is a better way to access struct members (the
points).
Accessing struct members with p1->p2->x seems rather ugly and unsafe.
Below is a stripped down version of the program, just to get an idea what
I'm trying to do. I'm aware that this is not a pure ANSI C definition
question, but I couldn't find any help to this special problem neither in
K&R nor in the FAQ. So I apreciate every hint.
Best Regards
Roman
#include <stdlib.h>
#include <stdio.h>
/* struct with points, used by struct profil */
struct point
{
double x;
double y;
};
struct profil
{
struct point *pt;
struct profil *previous;
struct profil *next;
};
int main(void)
{
int n;
struct profil *p1;
struct profil *begin;
n=2; /* only to allocate memory for this example */
if((p1=(struct profil *) malloc(sizeof(struct profil)))==NULL)
{
printf("\nMemory allocation failed\n");
return 1;
}
begin=p1;
if((p1->pt=(struct point *) malloc(n*sizeof(struct point)))==NULL)
{
printf("\nMemory allocation failed\n");
return 1;
}
/* put some information into the struct */
p1->pt->x=1265.75;
p1->pt->y=150.73;
p1->pt++; /* ugly, I know. Is there a better way to do this? */
p1->pt->x=550.55;
p1->pt->y=350.33;
p1->pt--;
/* do something with the structs */
/* instead of printing results, I print the structs out */
printf("\nStrukt 1: %f\n", p1->pt->x);
printf("\nStrukt 1: %f\n", p1->pt->y);
p1->pt++;
printf("\nStrukt 2: %f\n", p1->pt->x);
printf("\nStrukt 2: %f\n", p1->pt->y);
return 0;
}
I do have a question regarding structs. I have a struct (profil) which
has a pointer to another struct (point). The struct profil stores the
coordinates of points.
The problem is that I don't know how many points
there will be in every struct in the end, so I have to allocate memory
dynamically for
them and can't use an array of fixed size, unfortunately.
I would like to know if there is a better way to access struct members (the
points).
Accessing struct members with p1->p2->x seems rather ugly and unsafe.
Below is a stripped down version of the program, just to get an idea what
I'm trying to do. I'm aware that this is not a pure ANSI C definition
question, but I couldn't find any help to this special problem neither in
K&R nor in the FAQ. So I apreciate every hint.
Best Regards
Roman
#include <stdlib.h>
#include <stdio.h>
/* struct with points, used by struct profil */
struct point
{
double x;
double y;
};
struct profil
{
struct point *pt;
struct profil *previous;
struct profil *next;
};
int main(void)
{
int n;
struct profil *p1;
struct profil *begin;
n=2; /* only to allocate memory for this example */
if((p1=(struct profil *) malloc(sizeof(struct profil)))==NULL)
{
printf("\nMemory allocation failed\n");
return 1;
}
begin=p1;
if((p1->pt=(struct point *) malloc(n*sizeof(struct point)))==NULL)
{
printf("\nMemory allocation failed\n");
return 1;
}
/* put some information into the struct */
p1->pt->x=1265.75;
p1->pt->y=150.73;
p1->pt++; /* ugly, I know. Is there a better way to do this? */
p1->pt->x=550.55;
p1->pt->y=350.33;
p1->pt--;
/* do something with the structs */
/* instead of printing results, I print the structs out */
printf("\nStrukt 1: %f\n", p1->pt->x);
printf("\nStrukt 1: %f\n", p1->pt->y);
p1->pt++;
printf("\nStrukt 2: %f\n", p1->pt->x);
printf("\nStrukt 2: %f\n", p1->pt->y);
return 0;
}